
;all maps are 40x64 chars
;pcx's must be 320x512 pixels, greyscale

         ;map -> crunch
         ;gravity editor?
         ;uploader
         ;lights editor
         ;check pcx

display  = $30
displayh = $31
todisplay = $32
todisplayh = $33

lowin    = $40
highin   = $41
lowuit   = $42
highuit  = $43

xpixelsl = $45
xpixelsh = $46

linesl   = $47
linesh   = $48

bitmapl  = $49
bitmaph  = $4a

chars    = $4b
inchar   = $4c

namelow  = $4d
namehigh = $4e

screenlow = $4f
screenhigh = $50

bufferlow = $51
bufferhigh = $52

displayrow = $53

menulow  = $54
menuhigh = $55

low1     = $56
high1    = $57
low2     = $58
high2    = $59
low3     = $5a
high3    = $5b
startadressl = $5c
startadressh = $5d

highres =   $60
xpos    =   $61
ypos    =   $62
bitmaprow = $63
low  =      $64
high =      $65
color0400 = $66
colord800 = $67
temp =      $68
temp2 =     $69

         ;pd tool

         *= $0810

;---------------------------------------
	
	lda #0
	sta highres
menu
         jsr clearscreen
menuloop
         lda #0
         sta displayrow

         lda #<mainmenutext
         sta todisplay
         lda #>mainmenutext
         sta todisplayh
         jsr displaymenu

         lda #<mainmenuoptions
         sta menulow
         lda #>mainmenuoptions
         sta menuhigh
         jsr menutest

         jmp menuloop

menutest
         jsr $ffe4
         sta keyboardin
menutloop
         ldy #0
         lda (menulow),y
         cmp #$ff
         bne *+3
         rts
         cmp keyboardin
         beq menuj

         lda menulow
         clc
         adc #3
         sta menulow
         lda menuhigh
         adc #0
         sta menuhigh
         jmp menutloop
menuj
         iny
         lda (menulow),y
         sta menujump+1
         iny
         lda (menulow),y
         sta menujump+2
menujump
         jsr *
         rts

keyboardin .byte 0

mainmenuoptions
         .byte "i",<startpcx,>startpcx
         .byte "c",<startclear,>startclear
         .byte "l",<loadmap,>loadmap
         .byte "s",<savemap,>savemap
         .byte "b",<startbigmulti,>startbigmulti
         .byte "k",<krunchmap,>krunchmap
         .byte "r",<reverse,>reverse
         .byte "v",<viewhighres,>viewhighres
         .byte $ff

mainmenutext
         .text "pinball dreams editor "
         .text "v0.03 by wvl"
         .byte $00,$ff
         .byte $00,$ff
         .text "misc options :"
         .byte $00,$ff,$00,$ff
         .text "c - clear memory"
         .byte $00,$ff,$00,$ff
         .text "clipmap options :"
         .byte $00,$ff,$00,$ff
         .text "l - load map"
         .byte $00,$ff
         .text "s - save map"
         .byte $00,$ff
         .text "i - import pcx"
         .byte $00,$ff
         .text "r - reverse map"
         .byte $00,$ff
         .text "k - krunch map"
         .byte $00,$ff
         .text "v - view map"
         .byte $00,$ff
         .byte $00,$ff
         .text "b - convert koala to big multi"
         .byte 0,0

;---------------------------------------

startbigmulti
         inc $d021

	jsr clearscreen
bigmultil
         lda #0
         sta displayrow

         lda #<bigmultitext
         sta todisplay
         lda #>bigmultitext
         sta todisplayh
         jsr displaymenu

         lda #<bigoptions
         sta menulow
         lda #>bigoptions
         sta menuhigh
         jsr menutest

         jmp bigmultil

loadtop
         lda #<bitmap
         sta low1
         lda #>bitmap
         sta high1

         lda #<bitmap+$6000
         sta low2
         lda #>bitmap+$6000
         sta high2

         lda #<bitmap+$6c00
         sta low3
         lda #>bitmap+$6c00
         sta high3

         jmp loadkoala

loadmid
         lda #<bitmap+$1f40
         sta low1
         lda #>bitmap+$1f40
         sta high1

         lda #<bitmap+$6000+$03e8
         sta low2
         lda #>bitmap+$6000+$03e8
         sta high2

         lda #<bitmap+$6c00+$03e8
         sta low3
         lda #>bitmap+$6c00+$03e8
         sta high3

         jmp loadkoala

loadbot
         lda #<bitmap+$1f40+$1f40
         sta low1
         lda #>bitmap+$1f40+$1f40
         sta high1

         lda #<bitmap+$6000+$03e8+$03e8
         sta low2
         lda #>bitmap+$6000+$03e8+$03e8
         sta high2

         lda #<bitmap+$6c00+$03e8+$03e8
         sta low3
         lda #>bitmap+$6c00+$03e8+$03e8
         sta high3

         jmp loadkoala

loadkoala
	jsr clearscreen
         jsr getreadfile
         lda loadlength
         bne *+6
         jsr clearscreen
         rts
         jsr setinput

         jsr chrin
         jsr chrin
         jsr readst

         beq contloadkoa

         jsr closefile
         inc displayrow
         lda #<nofile
         sta todisplay
         lda #>nofile
         sta todisplayh
         jsr displaytext
         jsr waitforkey

         jmp loadkoala
contloadkoa
         ;laad bitmap data
         lda #<$1f40
         sta bytesread
         lda #>$1f40
         sta bytesread+1

         lda low1
         sta bufferlow
         lda high1
         sta bufferhigh
         jsr loadbytes

         ;laad col data

         lda #<$03e8
         sta bytesread
         lda #>$03e8
         sta bytesread+1

         lda low2
         sta bufferlow
         lda high2
         sta bufferhigh
         jsr loadbytes

         ;laad col data

         lda #<$03e8
         sta bytesread
         lda #>$03e8
         sta bytesread+1

         lda low3
         sta bufferlow
         lda high3
         sta bufferhigh
         jsr loadbytes

         jsr chrin
         sta papercolor
         lda #0
         sta bordercolor
         lda #$0b
         sta gridcolor

         jsr closefile
         jsr clearscreen

         rts
loadbytes
         lda bytesread
         bne loadbytes2
         lda bytesread+1
         bne loadbytes2
         lda #0
         sta $d020
         rts
loadbytes2
         jsr chrin
         sei
         ldy #$35
         sty $01

         ldy #0
         sta (bufferlow),y
         sta $d020

         ldy #$37
         sty $01
         cli
         lda bytesread
         sec
         sbc #$01
         sta bytesread
         lda bytesread+1
         sbc #0
         sta bytesread+1
         lda bufferlow
         clc
         adc #1
         sta bufferlow
         lda bufferhigh
         adc #0
         sta bufferhigh
         jmp loadbytes

savebig
         jsr clearscreen
         jsr getsavefile
         lda savelength
         bne *+6
         jsr clearscreen
         rts
         jsr setoutput

         lda #<bigmultistart
         jsr chrout
         lda #>bigmultistart
         jsr chrout

         jsr readst

         beq contsavebig

         jsr closefile
         inc displayrow
         lda #<saveerror
         sta todisplay
         lda #>saveerror
         sta todisplayh
         jsr displaytext
         jsr waitforkey

         jmp savebig
contsavebig
         lda #<bitmap
         sta low1
         lda #>bitmap
         sta high1
packstring
         ldx #$02
         ldy #$00
         lda (low1),y
         iny
         cmp (low1),y
         beq equalpack
         dex
         ;unequal
unequalpack
         lda (low1),y
         iny
         cmp (low1),y
         beq endunequal
         inx
         cpx #$7f
         bne unequalpack
endunequal
         txa
         pha
         asl a
         jsr chrout
         sta $d020

         ldy #0
woef
         lda (low1),y
         jsr chrout
         sta $d020
         iny
         dex
         bne woef

         pla
         clc
         adc low1
         sta low1
         bcc *+4
         inc high1
         jmp nextpack

equalpack
         iny
         cmp (low1),y
         bne endequal
         inx
         cpx #$7f
         bne equalpack
endequal
         pha
         txa
         sec
         rol a
         jsr chrout
         sta $d020
         pla
         jsr chrout
         sta $d020
         txa
         clc
         adc low1
         sta low1
         bcc *+4
         inc high1

         ;bla! nextbitpack
nextpack
         lda high1
         cmp #>bitmap+$7800
         bne packstring

         lda #0
         sta $d020
         jsr closefile
         jsr clearscreen
         rts

quitbig
         pla
         pla
         pla
         pla
         pla
         pla
         pla
         pla
         jmp menu

loadbig
         inc $d020
         rts

viewhighres
         lda #$c8
         sta $d016
         bne viewbmp2
viewbmp
         lda #$d8
         sta $d016
viewbmp2
         lda #0
         sta bitmaprow
         lda #<bitmap
         sta low1
         lda #>bitmap
         sta high1
         lda #<bitmap+$6000
         sta low2
         lda #>bitmap+$6000
         sta high2
         lda #<bitmap+$6c00
         sta low3
         lda #>bitmap+$6c00
         sta high3

         lda #$3b
         sta $d011
         lda #$94
         sta $dd00
         lda #$08
         sta $d018
         lda papercolor
         sta $d021
lp2
         jsr copybitmap
lp
         jsr $ffe4
         cmp #17
         bne *+8
         jsr cursordown
         jmp lp2

         cmp #145
         bne *+8
	jsr cursorup
	jmp lp2

         cmp #13
         beq endview

         bit $d011
         bmi *-3
         bit $d011
         bpl *-3
         jmp lp
endview
         jsr clearscreen

         rts
cursorup
         lda bitmaprow
         bne *+3
         rts
         dec bitmaprow
         lda low1
         sec
         sbc #$40
         sta low1
         lda high1
         sbc #1
         sta high1

         lda low2
         sec
         sbc #$28
         sta low2
         bcs *+4
         dec high2

         lda low3
         sec
         sbc #$28
         sta low3
         bcs *+4
         dec high3

         rts

cursordown
         lda bitmaprow
         cmp #64-25
         bne *+3
         rts

         inc bitmaprow
         lda low1
         clc
         adc #$40
         sta low1
         lda high1
         adc #1
         sta high1

         lda low2
         clc
         adc #$28
         sta low2
         bcc *+4
         inc high2

         lda low3
         clc
         adc #$28
         sta low3
         bcc *+4
         inc high3

         rts

copybitmap
         sei
         lda #$35
         sta $01

         lda #<$e000
         sta lowuit
         lda #>$e000
         sta highuit

         ldx #$20
         ldy #0
         lda (low1),y
         sta (lowuit),y
         iny
         bne *-5
         inc high1
         inc highuit
         dex
         bne *-12

         lda high1
         sec
         sbc #$20
         sta high1

         lda #<$c000
         sta lowuit
         lda #>$c000
         sta highuit

         ldx #$03
         ldy #0
         lda (low2),y
         sta (lowuit),y
         iny
         bne *-5
         inc high2
         inc highuit
         dex
         bne *-12
         lda (low2),y
         sta (lowuit),y
         iny
max0400  cpy #$f8
         bne *-7

         lda high2
         sec
         sbc #$03
         sta high2

         lda #<$d800
         sta lowuit
         lda #>$d800
         sta highuit

         ldx #$03
         ldy #0
         lda (low3),y
         sta (lowuit),y
         iny
         bne *-5
         inc high3
         inc highuit
         dex
         bne *-12
         lda (low3),y
         sta (lowuit),y
         iny
maxd800  cpy #$f8
	bne *-7

         lda high3
         sec
         sbc #$03
         sta high3

         lda #$37
         sta $01
         cli

         rts

bigoptions
         .byte "t",<loadtop,>loadtop
         .byte "m",<loadmid,>loadmid
         .byte "b",<loadbot,>loadbot
         .byte "s",<savebig,>savebig
         .byte "q",<quitbig,>quitbig
         .byte "v",<viewbmp,>viewbmp
         .byte "k",<startload,>startload
         .byte "a",<saveun,>saveun
         .byte "e",<swapcolors,>swapcolors
         .byte "f",<sort0400,>sort0400
         .byte "g",<savegamebitmap,>savegamebitmap
         .byte "u",<backgroundfill,>backgroundfill
         .byte $ff

bigmultitext
         .text "t - load top koala"
         .byte $00,$ff
         .text "m - load middle koala"
         .byte $00,$ff
         .text "b - load bottom koala"
         .byte $00,$ff
         .text "v - view bitmap"
         .byte $00,$ff
         .text " (l - load bigmulti,inactive)"
         .byte $00,$ff
         .text "s - save bigmulti"
         .byte $00,$ff
         .text "k - load unpacked"
         .byte $00,$ff
         .text "a - save unpacked"
         .byte $00,$ff
         .text "g - save game bitmaps"
         .byte $00,$ff
         .text " "
         .byte $00,$ff
         .text "f - sort $0400 colors"
         .byte $00,$ff
         .text "e - edit/swap colors"
         .byte $00,$ff
         .text "u - fill unused colors"
         .byte $00,$ff
         .text " "
         .byte $00,$ff
         .text "q - back to main menu"
         .byte 0,0

;---------------------------------------
reverse
         lda #<bitmap
         sta low1
         lda #>bitmap
         sta high1

         inc $d020

         ldy #0
reversl
         lda (low1),y
         eor #$ff
         sta (low1),y
         iny
         bne *-7
         inc high1
         lda high1
         cmp #>bitmap+$5000
         bne reversl

         dec $d020
         rts

loadmap
         jsr startload
         jsr setmapcolor
         rts

startload
         ;check start $2000

         jsr clearscreen
         jsr getreadfile
         lda loadlength
         bne *+6
         jsr clearscreen
         rts
         jsr setinput

         jsr chrin
         jsr chrin
         jsr readst

         beq contload

         jsr closefile
         inc displayrow
         lda #<nofile
         sta todisplay
         lda #>nofile
         sta todisplayh
         jsr displaytext
         jsr waitforkey

         jmp startload
contload
         lda #<bitmap
         sta bufferlow
         lda #>bitmap
         sta bufferhigh

         jsr sethighres
         jsr readloop
         jsr clearscreen
         rts

;---------------------------------------

savemap
         lda #<bitmap
         sta bufferlow
         sta startadressl
         lda #>bitmap
         sta bufferhigh
         sta startadressh

         lda #<$5000
         sta bytesread
         lda #>$5000
         sta bytesread+1

         jmp startsave

saveun
         lda #<bitmap
         sta bufferlow
         sta startadressl
         lda #>bitmap
         sta bufferhigh
         sta startadressh

         lda #<$7800
         sta bytesread
         lda #>$7800
         sta bytesread+1

         jmp startsave

startsave
         ;check start $2000

         jsr clearscreen
startsave2
         jsr getsavefile
         lda savelength
         bne *+6
         jsr clearscreen
         rts
startsave3
         jsr setoutput
         lda startadressl
         jsr chrout
         lda startadressh
         jsr chrout

         jsr readst

         beq contsave

         jsr closefile
         inc displayrow
         lda #<saveerror
         sta todisplay
         lda #>saveerror
         sta todisplayh
         jsr displaytext
         jsr waitforkey

         jmp startsave
contsave
         inc displayrow
         jsr writeloop
         jsr clearscreen
         rts

saveerror
         .text "error. file exists?"
         .byte 0

;---------------------------------------

startclear
         lda #<clearsure
         sta todisplay
         lda #>clearsure
         sta todisplayh

         jsr clearscreen
         jsr displaytext

         jsr $ffe4
         cmp #"y"
         beq *+7
         cmp #"n"
         bne *-9
         rts
         jsr clearbitmap
         rts
clearsure
         .text "clear memory? (y/n)"
         .byte 0

startpcx
         jsr clearscreen
         jsr getreadfile
         lda loadlength
         bne *+6
         jsr clearscreen
         rts
         jsr setinput

         jsr chrin
         jsr readst

         beq contpcx

         jsr closefile
         inc displayrow
         lda #<nofile
         sta todisplay
         lda #>nofile
         sta todisplayh
         jsr displaytext
         jsr waitforkey

         jmp startpcx
contpcx

         ;----------------
         ;thrash $7f bytes
         ;----------------

         ldy #$7f
         jsr chrin
         dey
         bne *-4

         inc displayrow
         jsr clearbitmap
         jsr sethighres
         lda #<bitmap
         sta bm
         lda #>bitmap
         sta bm+1

         ;--------

         lda #0
         sta linesl
         sta linesh

         ;--------
bigloop

         lda #$00
         sta lowuit
         lda #>tempdata
         sta highuit

         ;decode line

         lda #0
         sta xpixelsl
         sta xpixelsh
decodeloop
         jsr chrin
         sta $d020
         cmp #%11000000
         bcc notrle

         ;rle
         and #%00111111    ;aantal pixel
         tax
         jsr chrin
         sta $d020
         ldy #0
rle
         sta (lowuit),y

         iny
         dex
         bne rle       ;loop

         tya           ;calc # pixels
         clc
         adc xpixelsl
         sta xpixelsl
         lda xpixelsh
         adc #0
         sta xpixelsh

         tya           ;nieuwe uit
         clc
         adc lowuit
         sta lowuit
         lda highuit
         adc #0
         sta highuit

         jmp checkend

         ;notrle
notrle
         ldy #0
         sta (lowuit),y

         inc xpixelsl  ;+1 pixel
         bne *+4
         inc xpixelsh

         lda lowuit
         clc
         adc #1
         sta lowuit
         lda highuit
         adc #0
         sta highuit

checkend
         ;al 320 pixels gedecodeerd?

         lda xpixelsl
         cmp #$40
         bne decodeloop
         lda xpixelsh
         beq decodeloop

;to bitmap!!!

         lda linesl
         and #$07
         ora bm
         sta bitmapl
         lda bm+1
         sta bitmaph

         lda #0
         sta chars

         lda #<tempdata
         sta lowuit
         lda #>tempdata
         sta highuit
bitmaploop
         ldy #0
         ldx #0
         lda #0
         sta byte
completebyte
         lda (lowuit),y
         bpl notsetbit  ;not set=clip!

         lda byte       ;niet clip!
         ora tobit,x
         sta byte
notsetbit
         inx
         iny
         cpy #8
         bne completebyte

         ;schrijf byte

         ldy #0
         lda byte
         sta (bitmapl),y

         lda lowuit
         clc
         adc #$08
         sta lowuit
         bcc *+4
         inc highuit

         lda bitmapl
         clc
         adc #8
         sta bitmapl
         bcc *+4
         inc bitmaph

         inc chars
         lda chars
         cmp #40
         bne bitmaploop


         ;1 lijn meer

         lda linesl
         clc
         adc #1
         sta linesl
         lda linesh
         adc #0
         sta linesh

         lda linesl
         and #$07
         beq writeline

         ;bereken 8 lijnen

         jmp bigloop
writeline
         lda bm
         clc
         adc #$40
         sta bm
         lda bm+1
         adc #1
         sta bm+1

         lda linesl
         cmp #<512
         bne *+8
         lda linesh
         cmp #>512
         beq pcximported

         jmp bigloop

pcximported
         jsr closefile
         jsr setmapcolor
         lda #0
         sta $d020
         jsr clearscreen
         rts

bm
         .byte 0,0

byte
         .byte 0
tobit
         .byte $80,$40,$20,$10
         .byte $08,$04,$02,$01

;---------------------------------------

clearscreen
         lda #$97
         sta $dd00
         lda #$1b
         sta $d011
         lda #$c8
         sta $d016
         lda #$17
         sta $d018

         lda #0
         sta displayrow
         lda #11
         sta $d020
         sta $d021
         sta $0286
         jsr $e544
         lda #0
         sta $d020
         sta $d021
         rts

;---------------------------------------

sethighres

         lda #$3b
         sta $d011
         lda #$c8
         sta $d016
         lda #$18
         sta $d018
         ldx #0
         lda #$10
         sta $0400,x
         sta $0500,x
         sta $0600,x
         sta $0700,x
         inx
         bne *-13

         rts

setmapcolor
         lda #<bitmap+$6000
         sta low1
         lda #>bitmap+$6000
         sta high1

         ldy #0
setmcl
         lda #$10
         sta (low1),y
         iny
         bne *-3
         inc high1
         lda high1
         cmp #>bitmap+$7800
         bne setmcl
         rts

;---------------------------------------

getreadfile

         ;clearname

         ldx #$0f
         lda #$2e
         sta loadname,x
         dex
         bpl *-4
         lda #0
         sta loadlength

         lda #<pleasetext
         sta todisplay
         lda #>pleasetext
         sta todisplayh
         jsr displaytext

         ;enter loadname

         lda #<loadname
         sta namelow
         lda #>loadname
         sta namehigh

         lda display
         clc
         adc #$18
         sta screenlow
         lda displayh
         adc #0
         sta screenhigh

         jsr getname
         inc displayrow

         rts

;---------------------------------------

setdisplay
         ldx displayrow
         lda displaylow,x
         sta display
         lda displayhigh,x
         sta displayh
         rts

;---------------------------------------

setinput
         lda loadname+$10
         ldx #<loadname
         ldy #>loadname
         jsr setnam

         lda #$01
         ldx #$08
         ldy #$00
         jsr setlfs

         jsr open

         lda #1
         tax
         jsr chkin

         rts

;---------------------------------------

getsavefile

         ldx #$0f
         lda #$2e
         sta savename,x
         dex
         bpl *-4
         lda #0
         sta savelength

         lda #<pleasetext2
         sta todisplay
         lda #>pleasetext2
         sta todisplayh
         jsr displaytext

         lda #<savename
         sta namelow
         lda #>savename
         sta namehigh

         lda display
         clc
         adc #$18
         sta screenlow
         lda displayh
         adc #0
         sta screenhigh

         jsr getname
         inc displayrow
         rts

;---------------------------------------

displaytext
         jsr setdisplay
         ldy #0
         lda (todisplay),y
         beq *+7
         sta (display),y
         iny
         bne *-7
         rts

;---------------------------------------

displaymenu
         jsr displaytext
         inc displayrow
         iny
         lda (todisplay),y
         cmp #$ff
         beq *+3
         rts
         iny
         tya
         clc
         adc todisplay
         sta todisplay
         lda todisplayh
         adc #0
         sta todisplayh
         jmp displaymenu

;---------------------------------------

readloop
         lda #<bytereadtxt
         sta todisplay
         lda #>bytereadtxt
         sta todisplayh

         lda $d011
         and #$20
         bne *+5

         jsr displaytext

         lda #0
         sta bytesread
         sta bytesread+1
readloop2
         jsr chrin
         sta $d020

         sei
         ldx #$30
         stx $01

         ldy #$00
         sta (bufferlow),y

         ldx #$37
         stx $01
         cli

         lda bufferlow
         clc
         adc #$01
         sta bufferlow
         bcc *+4
         inc bufferhigh

         lda bytesread
         clc
         adc #$01
         sta bytesread
         bcc *+5
         inc bytesread+1

         lda $d011
         and #$20
         bne *+7

         ldy #$0e
         jsr displayread

         jsr readst
         and #$40
         beq readloop2

         jsr closefile

         inc displayrow
         lda #0
         sta $d020

         rts

;---------------------------------------

destdisk
         inc displayrow
         lda #<diskchange
         sta todisplay
         lda #>diskchange
         sta todisplayh
         jsr displaytext

         jsr waitforkey
         rts

;---------------------------------------

setoutput

         lda savename+$10
         ldx #<savename
         ldy #>savename
         jsr setnam

         lda #$01
         ldx #$08
         ldy #$01
         jsr setlfs

         jsr open

         lda #1
         tax
         jsr chkout
         rts

;---------------------------------------

writeloop
         lda #<bytewritetxt
         sta todisplay
         lda #>bytewritetxt
         sta todisplayh

         jsr displaytext

writeloop2
         sei
         ldx #$30
         stx $01

         ldy #$00
         lda (bufferlow),y

         ldx #$37
         stx $01
         cli

         sta $d020
         tax
         tay

         jsr chrout

         lda bufferlow
         clc
         adc #$01
         sta bufferlow
         bcc *+4
         inc bufferhigh

         lda bytesread
         sec
         sbc #$01
         sta bytesread
         bcs *+5
         dec bytesread+1

         ldy #$11
         jsr displayread

         lda bytesread+1
         bne writeloop2
         lda bytesread
         bne writeloop2

         jsr closefile

         inc displayrow
         lda #0
         sta $d020

         rts

;---------------------------------------

closefile
         jsr clrchn

         lda #$01
         tax
         jsr close

         rts

;---------------------------------------

waitforkey
         ldx #$10
         jsr $ffe4
         dex
         bpl *-4

         bit $d011
         bmi *-3
         bit $d011
         bpl *-3

         jsr $ffe4
         beq waitforkey+8
         rts

displayread
         lda bytesread+1
         lsr a
         lsr a
         lsr a
         lsr a
         tax
         lda onscreen,x
         sta (display),y
         lda bytesread+1
         and #$0f
         tax
         lda onscreen,x
         iny
         sta (display),y

         lda bytesread
         lsr a
         lsr a
         lsr a
         lsr a
         tax
         lda onscreen,x
         iny
         sta (display),y
         lda bytesread
         and #$0f
         tax
         lda onscreen,x
         iny
         sta (display),y

         rts

;---------------------------------------

getname
         ldy #15
         lda (namelow),y
         sta (screenlow),y
         dey
         bpl *-5
waitloop
         bit $d011
         bmi *-3
         bit $d011
         bpl *-3

         jsr $ffe4
         beq waitloop
         cmp #13
         beq entered

         cmp #20
         bne notdel

         ldy #$10
         lda (namelow),y
         beq waitloop
         sec
         sbc #$01
         sta (namelow),y
         tay
         lda #"."
         sta (screenlow),y
         jmp waitloop
notdel
         pha
         ldy #$10
         lda (namelow),y
         cmp #$10
         beq waitloop
         clc
         adc #$01
         sta (namelow),y
         tay
         dey
         pla
         sta (screenlow),y
         sta (namelow),y

         jmp waitloop

entered
         rts

loadname
         .text "................"
loadlength .byte 0
savename
         .text "................"
savelength .byte 0

bytesread
         .byte 0,0

;---------------------------------------

onscreen
         .text "0123456789abcdef"

bytereadtxt
         .text "bytes read : $...."
         .byte 0
bytewritetxt
         .text "bytes to save : $...."
         .byte 0
pleasetext
         .text "file to read : "
         .byte 0
pleasetext2
         .text "name file to save :"
         .byte 0
diskchange
         .text "please insert destinat"
         .text "ion disk,press key"
         .byte 0
nofile
         .text "error.. file not found?"
         .text " press key!"
         .byte 0


setnam   = $ffbd
setlfs   = $ffba
open     = $ffc0
chkin    = $ffc6
chrin    = $ffcf
chrout   = $ffd2
getbyte  = $ee13
chkout   = $ffc9
clrchn   = $ffcc
close    = $ffc3
readst   = $ffb7

displaylow
         .byte $00,$28,$50,$78
         .byte $a0,$c8,$f0
         .byte $18,$40,$68,$90
         .byte $b8,$e0
         .byte $08,$30,$58,$80
         .byte $a8,$d0,$f8
         .byte $20,$48,$70,$98,$c0
displayhigh
         .byte 4,4,4,4,4,4,4
         .byte 5,5,5,5,5,5
         .byte 6,6,6,6,6,6,6
         .byte 7,7,7,7,7

;-----------------------------------------------------

clearbitmap
	sei
	lda #$30
	sta $01

	lda #<bitmap
         sta lowin
         lda #>bitmap
         sta highin
clearbmploop
         ldy #0
         lda #0
         sta (lowin),y
         iny
         bne *-3

         inc highin
         lda highin
         cmp #$ff
         bne clearbmploop
         
         lda #$37
         sta $01
         cli

	rts

;----------------------------------------------------------

krunchmap
         jsr clearscreen

         sei
         lda #$35
         sta $01

         lda #<clipscreen
         sta low1
         lda #>clipscreen
         sta high1

         lda #<bitmap
         sta lowin
         lda #>bitmap
         sta highin
         lda #2            ;empty and filled char are the first in the charset
         sta usedchars

         ;set empty char and filled char already :)

         ldy #7
         lda #0
         sta clipcharset,y
         lda #$ff
         sta clipcharset+8,y
         dey
         bpl *-11

         ;take the current char in the bitmap
         ;and compare it with all the chars we added to the clipcharset already
bigcompareloop2
         ldx #0
         lda #<clipcharset
         sta lowuit
         lda #>clipcharset
         sta highuit
bigcompareloop
         ldy #7
compareloop
         lda (lowin),y
         cmp (lowuit),y
         bne checknextchar
         dey
         bpl compareloop
samecharfound
         txa              ;put the char
         ldy #0           ;that was already in the charset
         sta (low1),y     ;on the screen

         jmp nextchar

checknextchar
         ; if we got here, the char we checked was not the same, so we have to check the next one
         lda lowuit
         clc
         adc #8
         sta lowuit
         bcc *+4
         inc highuit

         inx
         cpx usedchars
         bne bigcompareloop
         
charnotfound
         ; we did not find the char, so it must be added

         ldy #7              ;copy the char to the charset
         lda (lowin),y
         sta (lowuit),y
         dey
         bpl *-5

         txa
         ldy #0              ;add the char to the screen
         sta (low1),y
         
         inc usedchars
         bne nextchar
         
         lda #2              ;too many chars used!!!
         sta $d020

         lda #$37
         sta $01
         cli

         lda #0
         sta displayrow
         lda #<toomanychars
         sta todisplay
         lda #>toomanychars
         sta todisplayh
         jsr displaytext

         jsr waitforkey

         lda #$00
         sta $d020
         
         rts

nextchar
         inc low1            ;next position on the screen
         bne *+4
         inc high1

         lda lowin
         clc
         adc #8
         sta lowin
         lda highin
         adc #0
         sta highin

         cmp #>bitmap+$5000
         bne bigcompareloop2

;----------------
         ;now we move chars at the start of rows to the end of the chars..

	;prepare

	lda #<clipscreen
	sta low1
	lda #>clipscreen
	sta high1

	lda usedchars
	asl
	asl
	asl
	sta low2
	lda usedchars
	lsr
	lsr
	lsr
	lsr
	lsr
	clc
	adc #>clipcharset
	sta high2
eatloop
	;--------

	ldy #0
	lda (low1),y
	sta temp

	asl
	asl
	asl
	sta lowuit

	lda temp
	lsr
	lsr
	lsr
	lsr
	lsr
	clc
	adc #>clipcharset
	sta highuit

	lda lowuit
	clc
	adc #8
	sta lowin
	lda highuit
	adc #0
	sta highin

         ;-------------------
         ;copy char to back -
         ;-------------------

         ldy #7
         lda (lowuit),y
         sta (low2),y
	dey
	bpl *-5

	;---------------------------
	;copy all chars 1 to front -
	;---------------------------

         lda usedchars
         sec
         sbc temp
	tax
	inx
pooploop
         dex
	beq stoppoop

         ldy #7
         lda (lowin),y
         sta (lowuit),y
	dey
	bpl *-5
	
	lda lowin
	clc
	adc #8
	sta lowin
	bcc *+4
	inc highin

	lda lowuit
	clc
	adc #8
	sta lowuit
	bcc *+4
	inc highuit

	jmp pooploop
stoppoop

;---------------------

	;we substract 1 from all the screen pointers above the original char...

         lda #<clipscreen
         sta lowin
         lda #>clipscreen
         sta highin

         ldx #$0a

         ldy #0
burritoloop

	lda (lowin),y

         cmp temp
         bne taco
         
	lda usedchars      ;moved char ==> last char
         sec
         sbc #1
         jmp tacobell
taco
         bcc tacobell       ;if char before moved char-> no change
         sec                ;otherwise substract one
         sbc #1

tacobell
         sta (lowin),y
	iny
	bne burritoloop

	inc highin
	dex
	bne burritoloop

	;next row
	
	lda low1
	clc
	adc #40
	sta low1
	bcc *+4
	inc high1
	
	lda high1
	cmp #>clipscreen+10*256
	beq *+5
	jmp eatloop


;----------------------

	;now we have to RLE crunch the clipscreen! (sigh)

         lda #<clipscreen
         sta lowin
         lda #>clipscreen
         sta highin
         
         lda #<krunchedclipscreen
         sta lowuit
         lda #>krunchedclipscreen
         sta highuit

         lda #<krunchedscreenpointers
         sta low1
         lda #>krunchedscreenpointers
         sta high1

         lda #0
         sta usedrows
         sta bytesremoved
         sta uniquerows

         ;check if we've seen the same row before (by comparing with all screenrows)
rowloop2
         ldx usedrows
         beq rownotusedbefore
         ldx #0

         lda #<clipscreen
         sta low2
         lda #>clipscreen
         sta high2
rowloop
         ldy #39
         lda (lowin),y
         cmp (low2),y
         bne checknextrow
         dey
         bpl *-7
         
         ;if we got here, the row is the same as one we crunched before

         txa
         asl
         tax
         ldy #0
         lda krunchedscreenpointers,x
         sta (low1),y
         lda krunchedscreenpointers+1,x
         iny
         sta (low1),y

         jmp nextrow


checknextrow
         lda low2           ;check the next row
         clc
         adc #40
         sta low2
         bcc *+4
         inc high2
         inx
         cpx usedrows
         bne rowloop

rownotusedbefore
         ;crunch line and insert it

	inc uniquerows

	;check if the last byte of the previous line =< the first byte of the new line
	;if so, move the pointers 1 byte back
	
	lda usedrows
	beq nopreviousbyte

	ldy #0
	lda (lowin),y
	dec highuit
	dey
	cmp (lowuit),y
	bcc notremovebyte

	lda lowuit
	sec
	sbc #1
	sta lowuit
	lda highuit
	sbc #0
	sta highuit
	inc bytesremoved

notremovebyte
	inc highuit

nopreviousbyte
         ldy #0              ;save pointer to krunchedlinedata :)
         lda lowuit
         sta (low1),y
         iny
         lda highuit
         sta (low1),y
         
         lda lowin
         sta pop1+1
         sta pop2+1
         lda highin
         sta pop1+2
         sta pop2+2

         ldx #0               ;write first byte to krunchedscreen
         ldy #0
	sty oldstart
pop2
	lda $1000,x
         sta (lowuit),y
         iny
         inx

pop1     cmp $1000,x
         bne stoprle
         inx
         cpx #40
         bne pop1
         inx              ;write 41 instead of 40
stoprle
         txa
         ;clc             ;make coordinates relative
         ;sbc oldstart
         ;stx oldstart    ;undo the relative thing for now...
         sta (lowuit),y
         iny
         cpx #41
         bne pop2
         
         tya
         clc
         adc lowuit
         sta lowuit
         bcc *+4
         inc highuit

nextrow
         lda low1              ;next row
         clc
         adc #2                ;means next pointerset
         sta low1
         lda lowin
         clc
         adc #40
         sta lowin
         bcc *+4
         inc highin            ;next clipscreenrow

         inc usedrows
         lda usedrows
         cmp #64
         beq *+5
         jmp rowloop2

         ;--------

         lda #$37
         sta $01
         cli

         lda #0
         sta displayrow
         lda #<charsused
         sta todisplay
         lda #>charsused
         sta todisplayh
         jsr displaytext

         lda usedchars
         lsr
         lsr
         lsr
         lsr
         tax
         lda onscreen,x
         sta $0401
         lda usedchars
         and #$0f
         tax
         lda onscreen,x
         sta $0402

	inc displayrow
	lda #<bytessavedtext
	sta todisplay
	lda #>bytessavedtext
	sta todisplayh
	jsr displaytext
         lda bytesremoved
         lsr
         lsr
         lsr
         lsr
         tax
         lda onscreen,x
         sta $042a
         lda bytesremoved
         and #$0f
         tax
         lda onscreen,x
         sta $042b

	inc displayrow
	lda #<linestext
	sta todisplay
	lda #>linestext
	sta todisplayh
	jsr displaytext
         lda uniquerows
         lsr
         lsr
         lsr
         lsr
         tax
         lda onscreen,x
         sta $042a+40
         lda uniquerows
         and #$0f
         tax
         lda onscreen,x
         sta $042b+40

savecharset
         inc displayrow
         lda #<clipcharset
         sta bufferlow
         sta startadressl
         lda #>clipcharset
         sta bufferhigh
         sta startadressh

         lda usedchars
         asl
         asl
         asl
         sta bytesread
         lda usedchars
         lsr
         lsr
         lsr
         lsr
         lsr
         sta bytesread+1

         jsr startsave2
saveclipscreen
         jsr clearscreen
         lda #0
         sta displayrow
         lda #<savescreen
         sta todisplay
         lda #>savescreen
         sta todisplayh
         jsr displaytext

         inc displayrow
         lda #<krunchedscreenpointers
         sta bufferlow
         sta startadressl
         lda #>krunchedscreenpointers
         sta bufferhigh
         sta startadressh

         lda lowuit
         sec
         sbc #<krunchedscreenpointers
         sta bytesread

         lda highuit
         sbc #>krunchedscreenpointers
         sta bytesread+1

         jsr startsave2
         rts

usedchars    .byte 0
usedrows     .byte 0
bytesremoved .byte 0
uniquerows   .byte 0
oldstart     .byte 0
toomanychars
         .text "too many chars used in the map!"
         .byte 0
charsused
         .text "$   chars used, saving charset"
         .byte 0
savescreen
         .text "saving crunched clip screen"
         .byte 0
bytessavedtext
	.text "#$.. end-bytes removed"
	.byte 0
linestext
	.text "#$.. unique lines"
	.byte 0
;----------------------------------------------

swapcolors
         lda #$c0
         sta max0400+1
         sta maxd800+1
	jsr makeswap

	lda #$01
	ldx #39
	sta $dbc0,x
	dex
	bpl *-4

	sei
         lda #$fc
         sta $d012
         lda #<switchirq2
         sta $0314
         lda #>switchirq2
         sta $0315
         lda #$01
         sta $d019
         sta $d01a
         sta $dc0d
         cli

	ldx #$3f
         lda cursor,x
         sta $c600,x
         lda #0
         sta $0600,x
	dex
	bpl *-12

	lda #$01
	sta $d015
	sta $d027
	lda #$00
	sta $d01c
	sta $d01d
	sta $d017
	sta xpos
	sta ypos

         lda #0
         sta bitmaprow
         lda #<bitmap
         sta low1
         lda #>bitmap
         sta high1
         lda #<bitmap+$6000
         sta low2
         lda #>bitmap+$6000
         sta high2
         lda #<bitmap+$6c00
         sta low3
         lda #>bitmap+$6c00
         sta high3

         lda #$3b
         sta $d011
         lda #$94
         sta $dd00
         lda #$08
         sta $d018
         lda papercolor
         sta $d021
lp3
         jsr copybitmap
         lda #$18
         sta $c3f8
         sta $07f8
lp4
         lda xpos
         asl
         asl
         asl
         clc
         adc #$18
         sta $d000
         
         lda xpos
         clc
         adc #3
         lsr
         lsr
         lsr
         lsr
         lsr
         sta $d010

	lda ypos
	sec
	sbc bitmaprow
	asl
	asl
	asl
	clc
	adc #50-13
	sta $d001

	jsr get0400color

	lda #24
	sta displayrow
	lda #<colortext
	sta todisplay
	lda #>colortext
	sta todisplayh
	jsr displaytext

	lda color0400
	lsr
	lsr
	lsr
	lsr
	tax
	lda onscreen,x
	sta $07c7
	lda color0400
	and #$0f
	tax
	lda onscreen,x
	sta $07c8
	
	lda colord800
	and #$0f
	tax
	lda onscreen,x
	sta $07d4

	jsr $ffe4
         
         cmp #157
         bne notleft
         
         ldx xpos
         beq notleft
         dec xpos
notleft
         cmp #29
         bne notright
         
         ldx xpos
         cpx #39
         beq notright
         inc xpos
notright
	cmp #17
         bne notdown

	ldx ypos
	cpx #63
	beq notdown
	inc ypos
	
	lda ypos
	sec
	sbc bitmaprow
	cmp #24
	bcc swaploop

	jsr cursordown2
	jmp lp3
notdown
         cmp #145
         bne notup

	ldx ypos
	beq notup
	dec ypos
	
	lda ypos
	sec
	sbc bitmaprow
	bpl swaploop
	
	jsr cursorup
	jmp lp3
notup
	cmp #"s"
	bne *+8
	jsr swap0400
	jmp lp3

	cmp #"d"
	bne *+8
	jsr swapd800
	jmp lp3

         cmp #13
         beq endview2
swaploop
         bit $d011
         bmi *-3
         bit $d011
         bpl *-3
         jmp lp4
endview2
         sei
         lda #<$ea31
         sta $0314
         lda #>$ea31
         sta $0315
         lda #0
         sta $d019
         lda #0
         sta $d01a
         lda #$81
         sta $dc0d
         cli

         lda #$e8
         sta max0400+1
         sta maxd800+1

	jsr clearscreen
	lda #$00
	sta $d015

         rts
cursordown2
         lda bitmaprow
         cmp #64-24
         bne *+3
         rts

         inc bitmaprow
         lda low1
         clc
         adc #$40
         sta low1
         lda high1
         adc #1
         sta high1

         lda low2
         clc
         adc #$28
         sta low2
         bcc *+4
         inc high2

         lda low3
         clc
         adc #$28
         sta low3
         bcc *+4
         inc high3

         rts

switchirq1
	asl $d019

	lda #$97
	sta $dd00
	lda #$17
	sta $d018
	lda #$1b
	sta $d011
	lda #$c8
	sta $d016

	lda #<switchirq2
	sta $0314
	lda #>switchirq2
	sta $0315
	lda #$fc
	sta $d012
	jmp $ea31


switchirq2
	asl $d019

	lda #$94
	sta $dd00
	lda #$08
	sta $d018
	lda #$d8
	sta $d016
	lda #$3b
	sta $d011

	lda #<switchirq1
	sta $0314
	lda #>switchirq1
	sta $0315
	lda #$f2
	sta $d012
	jmp $ea31

cursor
	.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
         .byte $ff,0,0,$81,0,0,$81,0,0,$81,0,0,$81,0,0,$81,0,0,$81,0,0,$ff,0,0

colortext  .text "0400 - .. / d800 - 0."
         .byte 0

get0400color
	lda #<$0000
	sta low
	lda #>$0000
	sta high
	
	ldx ypos
	cpx #0
	beq get04002
	lda low
	clc
	adc #40
	sta low
	bcc *+4
	inc high
	dex
	bne *-12
get04002
	lda low
	clc
	adc xpos
	sta low
	bcc *+4
	inc high

	lda high
	clc
	adc #>bitmap+$6000
	sta high
	
	ldy #0
	lda (low),y
	sta color0400

	lda high
	clc
	adc #>$0c00
	sta high

	lda (low),y
	sta colord800
	
	rts

swap0400
	jsr get0400color
	lda high
	sec
	sbc #>$0c00
	sta high
	lda color0400
	cmp #$80
	rol
	cmp #$80
	rol
	cmp #$80
	rol
	cmp #$80
	rol
	ldy #0
	sta (low),y
	sta color0400

	jsr pointtobitmap
	
	ldy #7
	lda (low),y
	tax
	lda swap0400tab,x
	sta (low),y
	dey
	bpl *-9
	rts
swapd800
	jsr get0400color
	lda high
	sec
	sbc #>$0c00
	sta high
	lda color0400
	tax
	and #$f0
	ora colord800
	sta color0400
	txa
	and #$0f
	sta colord800
	ldy #0
	lda color0400
	sta (low),y
	lda high
	clc
	adc #>$0c00
	sta high
	lda colord800
	sta (low),y
	
	jsr pointtobitmap
	
	ldy #7
	lda (low),y
	tax
	lda swapd800tab,x
	sta (low),y
	dey
	bpl *-9
	rts

pointtobitmap
	lda #0
	sta low
	lda #>bitmap
	sta high
	
	ldx ypos
	beq *+18
	lda low
	clc
	adc #$40
	sta low
	lda high
	adc #1
	sta high
	dex
	bne *-14
	
	lda xpos
	asl
	asl
	asl
	clc
	adc low
	sta low
	bcc *+4
	inc high
	
	lda xpos
	lsr
	lsr
	lsr
	lsr
	lsr
	clc
	adc high
	sta high
	rts

makeswap
	ldx #0

	stx temp

	ldy #3
makeswaploop
	asl temp
	rol temp2
	asl temp
	rol temp2

	lda temp2
	and #3
	cmp #%00000001
	bne makeswap2

	lda temp2
	and #%11111100
	ora #%00000010
	sta temp2
	bne makeswap3
makeswap2
	cmp #%00000010
	bne makeswap3

	lda temp2
	and #%11111100
	ora #%00000001
	sta temp2
makeswap3
	dey
	bpl makeswaploop

	lda temp2
	sta swap0400tab,x
makeswap10
	stx temp
	ldy #3
makeswaploop2
	asl temp
	rol temp2
	asl temp
	rol temp2

	lda temp2
	and #3
	cmp #%00000011
	bne makeswap4

	lda temp2
	and #%11111100
	ora #%00000010
	sta temp2
	bne makeswap5
makeswap4
	cmp #%00000010
	bne makeswap5

	lda temp2
	and #%11111100
	ora #%00000011
	sta temp2
makeswap5
	dey
	bpl makeswaploop2

	lda temp2
	sta swapd800tab,x

	inx
	bne makeswap+2

	rts

;---------------------------------------------------------------------------------------

sort0400
	dec $d020

	jsr makeswap

	lda #<bitmap
	sta low1
	lda #>bitmap
	sta high1
	
	lda #<bitmap+$6000
	sta low2
	lda #>bitmap+$6000
	sta high2
sort0400loop
	ldy #0
	lda (low2),y
	cmp #$80
	rol
	cmp #$80
	rol
	cmp #$80
	rol
	cmp #$80
	rol
	cmp (low2),y
	bcs sort04002
	sta (low2),y

	ldy #7
	lda (low1),y
	tax
	lda swap0400tab,x
	sta (low1),y
	dey
	bpl *-9
sort04002
	lda low1
	clc
	adc #8
	sta low1
	bcc *+4
	inc high1
	
	inc low2
	bne *+4
	inc high2
	
	lda high1
	cmp #>bitmap+$6000
	bne sort0400loop

	inc $d020

	rts

;--------------------------------------------------------------------

backgroundfill

        inc $d020

        lda papercolor
        and #$f
        sta papercolor
        asl
        asl
        asl
        asl
        sta papercolor2

	lda #<bitmap          ;bitmap
	sta low1
	lda #>bitmap
	sta high1

	lda #<bitmap+$6000    ;0400 colors
	sta low2
	lda #>bitmap+$6000
	sta high2

        lda #<bitmap+$6c00    ;d800 colors
        sta low3
        lda #>bitmap+$6c00
        sta high3

	; check for each char if there are unused colors
bigbackgroundfillloop	
	ldy #0
	sty fill1
	sty fill2
	sty fill3
        lda (low1),y         ;get byte
backgroundfillloop
        pha
	and #%00000011
	tax
	inc fill0,x
	pla
	lsr
	lsr
	bne backgroundfillloop
        iny
        cpy #8
        bne backgroundfillloop-2
        
        ;here one char is checked.
        ;now we fill unused colors with background color

        lda fill1
        bne col01used

        ldy #0
        lda (low2),y
        and #$0f
        ora papercolor2
        sta (low2),y

col01used
        lda fill2
        bne col10used
        
        ldy #0
        lda (low2),y
        and #$f0
        ora papercolor
        sta (low2),y

col10used
        lda fill3
        bne col11used

        ldy #0
        lda papercolor
        sta (low3),y
col11used
        ;next char

        inc low2
        bne *+4
        inc high2

        inc low3
        bne *+4
        inc high3

        lda low1
        clc
        adc #8
        sta low1
        bne *+4
        inc high1
        
        lda high1
        cmp #>(bitmap+$5000)
        bne bigbackgroundfillloop
        
        dec $d020
        rts

fill0 .byte 0 ;%00
fill1 .byte 0 ;%01
fill2 .byte 0 ;%10
fill3 .byte 0 ;%11

papercolor2 .byte 0
;--------------------------------------------------------------------

savegamebitmap
	jsr clearscreen

	lda #<bitmap+$6c00
	sta low1
	lda #>bitmap+$6c00
	sta high1
	
	lda #<bitmap+$6c00+20
	sta low2
	lda #>bitmap+$6c00+20
	sta high2
	
	lda #<$c000
	sta low3
	lda #>$c000
	sta high3
saveprepd800
	ldy #19
	lda (low2),y
	asl
	asl
	asl
	asl
	ora (low1),y
	sta (low3),y
	dey
	bpl *-11
	
	lda low1
	clc
	adc #40
	sta low1
	bcc *+4
	inc high1

	lda low2
	clc
	adc #40
	sta low2
	bcc *+4
	inc high2

	lda low3
	clc
	adc #20
	sta low3
	bcc *+4
	inc high3

	lda high3
	cmp #>$c000+$500
	bne saveprepd800
	
	lda #0
	sta temp
savegameloop
         inc $d020

	lda temp
	asl
	asl
	asl
	asl
	tax
	ldy #0
	lda gamenames,x
	sta savename,y
	inx
	iny
	cpy #$10
	bne *-10

	ldx temp
	lda gamenamelengths,x
	sta savelength

	lda temp
	asl
	tax
	lda gamestarts,x
	sta startadressl
	lda gamestarts+1,x
	sta startadressh

	lda gamelengths,x
	sta bytesread
	lda gamelengths+1,x
	sta bytesread+1

	lda memstart,x
	sta bufferlow
	lda memstart+1,x
	sta bufferhigh

	jsr startsave3

	inc temp
	lda temp
	cmp #11
	bne savegameloop

	lda #0
	sta $d020

	rts

gamenames
	.text "bmp0............"
	.text "bmp1a..........."
	.text "bmp1b..........."
	.text "bmp2a..........."
	.text "bmp2b..........."
	.text "bmp0col........."
	.text "bmp1cola........"
	.text "bmp1colb........"
	.text "bmp2cola........"
	.text "bmp2colb........"
	.text "d800col........."
gamenamelengths
	.byte 4,5,5,5,5,7,8,8,8,8,7
gamestarts
	.word $6000  ;...7f40
	.word $a000  ;...be80
	.word $bf40  ;...c000
	.word $c000  ;...d000
	.word $de80  ;...e000

	.word $5c00
	.word $8c00
	.word $8fe8
	.word $e000
	.word $e3d0

	.word $8600
gamelengths
         .word $1f40
         .word $1e80
         .word $00c0
         .word $1000
         .word $0180

	.word $03e8
	.word $03d0
	.word $0018
	.word $0200
	.word $0030

	.word $0500
memstart
	.word bitmap
	.word bitmap+$2000
	.word bitmap+$1f40
	.word bitmap+$4000
	.word bitmap+$3e80

	.word bitmap+$6000
	.word bitmap+$6000+$400
	.word bitmap+$6000+$3e8
	.word bitmap+$6000+$800
	.word bitmap+$6000+$7d0

	.word $c000


tempdata      = $c000
bitmap        = $2000 ;->7000
papercolor    = bitmap+$77e0
bordercolor   = papercolor+1
gridcolor     = bordercolor+1
bigmultistart = $2000

clipcharset            = $9800
clipscreen             = $a000
krunchedscreenpointers = $c000
krunchedclipscreen     = $c080
swap0400tab            = $c900
swapd800tab            = $ca00





;bitmapcolors run up to $2000+$7800 = $9800


;bitmap during view : colors $c000
;                     bitmap $e000
;

