;-------------------------------------------------------------------------------
;Loader part
;-------------------------------------------------------------------------------
;by Pex 'Mahoney' Tufvesson, 9th of March 2004
;Memory map: In the file "globals.s"

  include globals.s
  include build_number.s

  processor 6502

 seg code
 org $0400

bn0 = (build_number / 10000) & $f)
bn1 = ((build_number - bn0*10000) / 1000) & $f)
bn2 = ((build_number - bn0*10000 - bn1*1000) / 100) & $f)
bn3 = ((build_number - bn0*10000 - bn1*1000 -bn2*100) / 10) & $f)
bn4 = ((build_number - bn0*10000 - bn1*1000 -bn2*100 - bn3*10)) & $f)
 
  dc.b bn0 + '0
  dc.b bn1 + '0
  dc.b bn2 + '0
  dc.b bn3 + '0
  dc.b bn4 + '0
  dc.b $1f,$02,$15,$09
  dc.b $0c,$04,$20,$0e,$15,$0d,$02,$05
  dc.b $12,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$14,$08,$09,$13,$20,$09,$13
  dc.b $20,$14,$08,$05
  dc.b $20,$06,$09,$0e,$01,$0c,$20,$16
  dc.b $05,$12,$13,$09,$0f,$0e,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$08,$01,$16,$05,$20,$01,$20
  dc.b $0e,$0f,$09,$13,$05,$20,$0e,$09
  dc.b $07,$08,$14,$21,$20,$2f,$0d,$01
  dc.b $08,$0f,$0e,$05,$19,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$08,$14,$14,$10,$3a,$2f,$2f
  dc.b $0d,$01,$08,$0f,$0e,$05,$19,$2e
  dc.b $03,$36,$34,$2e,$0f,$12,$07,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
  dc.b $20,$20,$20,$20,$20,$20,$20,$20
;$0520:
  ds.b $e0,$20
;$0600:
  ds.b $100,$20
;$0700:
  ds.b $100,$20


  seg code
  org $0801

  dc.b $0b,$08           ;Address of next BASIC instruction
  dc.w 2004              ;Line number
  dc.b $9e               ;SYS-token
  dc.b $32,$30,$36,$31   ;2061 in ASCII
  dc.b $00,$00,$00       ;BASIC program end

start:
  lda #$7f       ; turn off the CIA interrupts
  sta $dc0d
  sta $dd0d ;Clear NMI
  sei
  cld
  ldx #$ff
  txs
.wait_screen2:
  lda $d012
  cmp #$a0
  bne .wait_screen2
.wait_screen3:
  lda $d012
  cmp #$30
  bne .wait_screen3

  ldx #0
  lda #$6
.blueback:
  sta $da00,x
  sta $da80,x
  inx
  bne .blueback

;Initialize music:
  lda #0
  jsr MUSIC_INIT

;Used in c4.s to Decrunch an image inside of the part.
  lda #$4c     ;jmp
  sta decrunchimage
  lda #<Decrunch_an_image
  sta decrunchimage+1
  lda #>Decrunch_an_image
  sta decrunchimage+2

  jsr FirstLogo_init
;Music ok if stuck here.  
  jsr initloader
;Music ok if stuck here.  
  
  jsr Before_a_part

;  jsr After_a_partInit
;  jsr set_defd000


  ldx #$fe
  stx this_sign
  jsr Load_an_image

;  jsr Clear_screen

  jsr After_a_part

from_the_beginning:

;### TESTING AREA
;  lda #8   ;spriteplot
;  jsr Load_a_part
;  jsr swap_music
;  jsr Before_a_part
;  jsr $7000
;  jsr set_defd000
;  jsr After_a_part
;  jmp from_the_beginning
;###


;### TESTING AREA
;  lda #7   ;The end
;  jsr Load_a_part
;  jsr swap_music
;  jsr Before_a_part
;  jsr $7000
;  jsr set_defd000
;  jsr After_a_part
;  jmp from_the_beginning
;###

;### TESTING AREA
;  lda #3  ;Left borderscroll
;  jsr Load_a_part
;  jsr Before_a_part
;  lda #0
;  jsr $8000
;  jsr After_a_partInit
;###

;### TESTING AREA
;  lda #5  ;Stretch
;  jsr Load_a_part
;  jsr Before_a_part
;  lda #2
;  jsr PART5_START
;  jsr After_a_partInit
;###

  lda #4  ;Stretching logos
  jsr Load_a_part
  jsr Before_a_part
  lda #0
  jsr $7000
  jsr After_a_partInit
  jsr Load_an_image
  lda #2
  jsr $7000
  jsr After_a_partInit
  jsr Load_an_image

;  jsr After_a_part

  lda #9  ;Lines
  jsr Load_a_part
  jsr Before_a_part
  lda #0
  jsr $7000
  jsr After_a_line

  lda #5  ;fullscreen sprites
  jsr Load_a_part
  jsr Before_a_part
  lda #0
  jsr PART5_START
  jsr set_defd000
  jsr After_a_part

  jsr After_a_part

  lda #9  ;Lines
  jsr Load_a_part
  jsr Before_a_part
  lda #1
  jsr $7000
  jsr After_a_line

  jsr After_a_part

;  lda #9  ;Lines
;  jsr Load_a_part
  jsr Before_a_part
  lda #2
  jsr $7000
  jsr After_a_line

  jsr After_a_part

  lda #8 ;spriteplot
  jsr Load_a_part
  jsr Before_a_part
  jsr $7000
  lda #$3b
  sta $d011
  lda #$0
  sta $d012
  jsr set_defd000
  jsr After_a_part

  jsr After_a_part

  lda #9  ;Lines
  jsr Load_a_part
  jsr Before_a_part
  lda #3
  jsr $7000
  jsr After_a_line

  jsr After_a_part

;  lda #9  ;Lines
;  jsr Load_a_part
  jsr Before_a_part
  lda #4
  jsr $7000
  jsr After_a_line

  jsr After_a_part

  lda #9  ;Lines
  jsr Load_a_part
  jsr Before_a_part
  lda #5
  jsr $7000
  jsr After_a_line

  lda #3  ;Train in left border
  jsr Load_a_part
  jsr Before_a_part
  lda #0
  jsr $8000
  jsr After_a_partInit

  jsr After_a_part

  lda #5  ;fullscreen sprites
  jsr Load_a_part
  jsr Before_a_part
  lda #1
  jsr PART5_START
  jsr set_defd000
  jsr After_a_part

  jsr After_a_part

  lda #9  ;Lines
  jsr Load_a_part
  jsr Before_a_part
  lda #6
  jsr $7000
  jsr After_a_line

  jsr After_a_part

;  lda #9  ;Lines
;  jsr Load_a_part
  jsr Before_a_part
  lda #7
  jsr $7000
  jsr After_a_line

  jsr After_a_part

;  lda #9  ;Lines
;  jsr Load_a_part
  jsr Before_a_part
  lda #8
  jsr $7000
  jsr After_a_line

  jsr After_a_part

;  lda #9  ;Lines
;  jsr Load_a_part
  jsr Before_a_part
  lda #9
  jsr $7000
  jsr After_a_line

  jsr After_a_part

;  lda #9  ;Lines
;  jsr Load_a_part
  jsr Before_a_part
  lda #10
  jsr $7000
  jsr After_a_line

  jsr After_a_part

  lda #5  ;fullscreen sprites
  jsr Load_a_part
  jsr Before_a_part
  lda #2
  jsr PART5_START
  jsr set_defd000
  jsr After_a_part

  jsr After_a_part

  lda #9  ;Lines
  jsr Load_a_part
  jsr Before_a_part
  lda #11
  jsr $7000
  jsr After_a_line

  lda #7   ;The end
  jsr Load_a_part
  jsr swap_music
  jsr Before_a_part
  jsr $7000
  jsr set_defd000
  jsr After_a_part
  jmp from_the_beginning

Load_a_part: subroutine
;  lda #3
;  sta $d020
  clc
  adc #'0
  sta part_no
  ldx #<filename
  ldy #>filename
  jsr loadfile_pucrunch   ;Load file
  bcc ok2
  sta $d020               ;If error, show errorcode in border
exit2:
  jsr getin
  tax
  beq exit2
  jmp 64738
ok2:
  rts

saved_zp:
  ds.b 256,0

Before_a_part:
;  lda #0
;  sta $d020
;  lda #0
;  sta $d021
  ldx #$80
.mor2:
  lda $00,x
  sta saved_zp,x
  inx
  bne .mor2
;.wait_screen:
;  lda $d012
;  cmp #$a0
;  bne .wait_screen
  rts

After_a_line:
;  sei
;  lda #$f0
;.weit:
;  cmp $d012
;  bne .weit
  jsr After_a_partInit
  jsr Decrunch_an_image
  jmp Load_an_image

After_a_part:
  jsr Clear_screen
  jsr After_a_partInit
  jsr Decrunch_an_image
  jmp Load_an_image

Clear_screen:
  ldx #0
.clr_screen:
  lda #$11
  sta $6000,x
  sta $6100,x
  sta $6200,x
  sta $62f7,x
  lda #1
  sta $d800,x
  sta $d900,x
  sta $da00,x
  sta $daf8,x
  inx
  bne .clr_screen
  lda #0
  sta $3fff
;  sta $7fff
;  lda #$3b
;  sta $d011
  rts

After_a_partInit:
  lda #$7f       ; turn off the CIA interrupts
  sta $dc0d
  sta $dd0d ;Clear NMI
  sei
  
;Music bugs out after this routine:
;Probably, GoatTracker uses some other regs than $fd+$fe for counters in zp
;  ldx #$80
;.mor:
;  lda saved_zp,x
;  sta $00,x
;  inx
;  cpx #$f8
;  bne .mor

  lda #$81
  sta $dd0d     ; enable CIA irq
  ldx #$00
  stx $d01a     ; disable raster interrupt
  lda $dc0d     ; acknowledge CIA interrupts
;  lda $dd0d     ; acknowledge CIA interrupts
  lsr $d019     ; and video interrupts

;.wait_screen:
;  lda $d012
;  cmp #$a0
;  bne .wait_screen
;  lda #$3b
;  sta $d011
  lda #0
  sta loaderirq_cou
  lda #<irq1
  sta $0314
  lda #>irq1
  sta $0315
  lda #<irq1_no_kernel
  sta $fffe
  lda #>irq1_no_kernel
  sta $ffff
  lda #<nmi
  sta $fffa
  lda #>nmi
  sta $fffb
  lda #irq1_raster
  sta $d012
  lda $d011
  and #$78
  ora #$03
  sta $d011
  lda #$7f
  sta $dc0d     ; disable timer interrupts
  sta $dd0d
  lda #1
  sta $d01a     ; enable raster interrupt
  lda $dc0d     ; acknowledge CIA interrupts
  lsr $d019     ; and video interrupts
  lda #$37
  sta $01
  cli

  lda #$1
  sta $d020
  sta $d021
;.wait_screen:
;  lda loaderirq_cou
;  beq .wait_screen

;  lda #%00000010
;  sta $dd00
  lda #$c8
  sta $d016
;  lda #$85  ;screen at $6000, spr data at $63f8
;  sta $d018
;  lda #$2
;  sta $dd00
  lda #$ff
  sta $d015
  lda #$c8
  sta $d016
  lda #$00
  sta $d01b ;Spr-background priority

  ldx #$c6   ;$3180
  ldx #$d0
  stx $07fe
  stx $07ff
  inx
  stx $07fa
  inx
  stx $07fb
  inx
  stx $07fc
  inx
  stx $07fd
  inx
  stx $07f8
  inx
  stx $07f9

  jmp set_sprites

set_defd000:
  ldx #$2f
.morrr:
  lda after_d000,x
  sta $d000,x
  dex
  cpx #$12
  bne .morrr
;  rts

set_sprites:
  lda #$03
  sta $d010
  lda #$c0
  sta $d017
  sta $d01b
  lda #$fc
  sta $d00f
  sta $d00d
  sta $d007
  sta $d005
  lda #$04
  sta $d001
  sta $d003
  lda #$11
  sta $d009
  sta $d00b
;spr 0+1 x-value is set in the interrupt
  lda #$30
  sta $d004
  lda #$48
  sta $d006
  lda #$30
  sta $d008
  lda #$48
  sta $d00a
  lda #$18
  sta $d00c
  lda #$48
  sta $d00e
  lda #$05
  sta $d027
  sta $d028
  sta $d02d
  sta $d02e
  lda #1
  sta $d029
  sta $d02a
  sta $d02b
  sta $d02c
  rts

after_d000:
 dc.b $10,$04,$28,$04,$30,$fc,$48,$fc
 dc.b $30,$11,$48,$11,$18,$fc,$48,$fc
 dc.b $03,$3b,$f9,$00,$00,$ff,$c8,$c0
 dc.b $85,$70,$f1,$00,$00,$c0,$ff,$00
 dc.b $f1,$f1,$f0,$f0,$f0,$f0,$f0,$05
 dc.b $05,$01,$01,$01,$01,$05,$05

;-------------------------------------------------------------------------------
; Configuration with PUCRUNCH loading
;-------------------------------------------------------------------------------

LONG_NAMES      = 1             ;Set to nonzero to use long names (pointer in
                                ;X,Y) or zero to use 2-letter names (letters
                                ;in X,Y)
BORDER_FLASHING = 0             ;Set to nonzero to enable border flashing
                                ;when fastloading :)
ADDITIONAL_ZEROPAGE = 0         ;Set to nonzero to use additional zeropage
                                ;variables to shorten loader code
LOAD_UNDER_IO   = 1             ;Set to nonzero to enable possibility to load
                                ;under I/O areas, and to load packed data
                                ;under the Kernal ROM.
NTSC_RASTER_DETECTION = 1       ;Set to nonzero to detect PAL/NTSC by measuring
                                ;greatest rasterline number, or to zero, to
                                ;rely on the value reported in $02a6
REU_SUPPORT     = 0             ;Set to one to include REU+SuperRAM buffering
                                ;support
SLOWLOAD_SUPPORT = 0            ;Set to one to include loading using Kernal
HISCORE_SUPPORT = 0             ;Set to nonzero to include hiscore load/save
LOADFILE_UNPACKED = 1           ;Set to nonzero to include unpacked loading
LOADFILE_EXOMIZER = 0           ;Set to nonzero to include EXOMIZER loading
LOADFILE_PUCRUNCH = 1           ;Set to nonzero to include PUCRUNCH loading

RETRIES         = 5             ;Retries when reading a sector

loadbuffer      = $0680         ;256 byte table used by fastloader & REU

depackbuffer    = $0780         ;156 bytes for EXOMIZER tables, 31 for
                                ;PUCRUNCH.

zpbase          = $02           ;Zeropage base address. Loader needs 2
                                ;addresses with unpacked, 3 with PUCRUNCH
                                ;and 8 with EXOMIZER loading.

zpbase2         = $06           ;Additional 4 zeropage (6 with REU buffering)
                                ;addresses for shortening the loader code
                                ;(optional)

                include lo_loader.s


irq1_raster = $f9

irq1_no_kernel:
  pha
  txa
  pha
  tya
  pha

irq1:
 ; lda #13
 ; sta $d020
 ; lda #13
 ; sta $d021
 ; lda #$3b
 ; sta $d011

  lda $dd00
  and #$fc
  ora #$03
  tax

  lda $d011
  and #$17
  sta $d011
  stx $dd00

  asl $d019
  lda #$ff
  sta $d015
  lda #0
  sta $3fff

  lda #$15  ;screen at $0400, spr data at $07f8
  sta $d018

  lda #$01
  sta $d021
  lda #$01
  sta $d020
  lda #$c0
  sta $d017
  sta $d01d

  lda music_on
  beq .nomus
  jsr MUSIC_PLAY
  jmp .cont
.nomus:
  ldx #$a0
.loo:
  dex
  bne .loo
.cont:
  lda $d011
  and #$7f
  ora #$18
  sta $d011
;  ldy #0
;.lo:
;  dey
;  bne .lo

  lda #<irq2
  sta $0314
  lda #>irq2
  sta $0315
  lda #irq2_raster
  sta $d012
  inc loaderirq_cou

  pla
  tay
  pla
  tax
  pla
nmi:
  rti

irq2_raster = $00
irq2:
  lda #$00
  sta $d015
  asl $d019
  lda #<irq3
  sta $0314
  lda #>irq3
  sta $0315
  lda #irq3_raster
  sta $d012
  lda #$85  ;screen at $6000, spr data at $63f8
  sta $d018
  pla
  tay
  pla
  tax
  pla
  rti

irq3_raster = $30
irq3:
;  lda #$01
;  sta $d020


  ldy #$32
.muu:
  cpy $d012
  bne .muu

  asl $d019
  lda #<irq1
  sta $0314
  lda #>irq1
  sta $0315
  lda #irq1_raster
  sta $d012
  nop
  nop
  nop

  lda $d011
  and #$7f
  ora #$28
  tax

  lda $dd00
  and #$fc
  ora #$02
  sta $dd00
  stx $d011

  ldx exitbounce
  lda sintbl,x
  clc
  adc #$80
  lsr
  lsr
  lsr
  lsr
  clc
  adc #$18
  sta $d000
  clc
  adc #$18
  sta $d002
;  lda sintbl+$35,x
;  clc
;  adc #$80
;  lsr
;  lsr
;  lsr
;  lsr
;  lsr
;  sta $d001
;  sta $d003
  inc exitbounce

  pla
  tay
  pla
  tax
  pla
  rti




filename:       dc.b "PART "
part_no:        dc.b "1",0

sign_filename:
  dc.b "SIGN"
sign_no:
  dc.b "01",0
sign_numbers:
  dc.b "010203040506070809101112131415161718192021222324252627282930313233"

this_sign: dc.b $fe

Load_an_image: subroutine
  inc this_sign
  inc this_sign
  ldx this_sign
  cpx #33*2
  bne .noww
  ldx #0
  stx this_sign
.noww:
  lda sign_numbers,x
  sta sign_no
  lda sign_numbers+1,x
  sta sign_no+1
;  lda #6
;  sta $d020

image_packed = $6800

;  lda #<image_packed
;  sta destlo
;  lda #>image_packed
;  sta desthi

  ldx #<sign_filename
  ldy #>sign_filename
  jsr loadfile   ;Load file
  bcc .ok3
  sta $d020               ;If error, show errorcode in border
.exit3:
  jsr getin
  tax
  beq .exit3
  jmp 64738
.ok3:
;  lda #$3b
;  sta $d011
  rts

Decrunch_an_image: subroutine
  ldx #>image_packed
  ldy #<image_packed
  jmp decrunch

;############## Music changer

music_on: dc.b $1
music_filename:
  dc.b "MUSIC2",0

music2_packed = $22da;

swap_music: subroutine
  lda #0
  sta music_on
  lda #$0
  sta $d418
  ldx #<music_filename
  ldy #>music_filename
  jsr loadfile_pucrunch   ;Load file
  bcc .ok3
  sei
  sta $d020               ;If error, show errorcode in border
.exit3:
  jsr getin
  tax
  beq .exit3
  jmp 64738
.ok3:

;Decrunch_music:
  lda #0
  jsr MUSIC_INIT
  lda #1
  sta music_on
  rts

;############## First logo

FirstLogo_init:
  lda #$7f       ; turn off the CIA interrupts
  sta $dc0d
  sta $dd0d ;Clear NMI
  sei
  lda #$81
  sta $dd0d     ; enable CIA irq
  ldx #$00
  stx $d01a     ; disable raster interrupt
  lda $dc0d     ; acknowledge CIA interrupts
  lda $dd0d     ; acknowledge CIA interrupts
  lsr $d019     ; and video interrupts

.wait_screen:
  lda $d012
  cmp #$a0
  bne .wait_screen
;  lda #$3b
;  sta $d011
  lda #<firstirq1
  sta $0314
  lda #>firstirq1
  sta $0315
  lda #<nmi
  sta $fffa
  lda #>nmi
  sta $fffb
  lda #firstirq1_raster
  sta $d012
  lda #$1b
  sta $d011
  lda #$7f
  sta $dc0d     ; disable timer interrupts
  sta $dd0d
  lda #1
  sta $d01a     ; enable raster interrupt
  lda $dc0d     ; acknowledge CIA interrupts
  lsr $d019     ; and video interrupts
  lda #$37
  sta $01
  cli

  ldx #0
.setcols:
  lda $6400,x
  sta $d800,x
  lda $6500,x
  sta $d900,x
  dex
  bne .setcols

  lda #$0
  sta $d020
  sta $d021
  lda #$c8
  sta $d016
  lda #$00
  sta $d015
  lda #$00
  sta $d01b ;Spr-background priority
  rts


firstirq1_raster = $f9

firstirq1_no_kernel:
  pha
  txa
  pha
  tya
  pha
firstirq1:
  lda $d011
  and #$7f
  ora #$28
  tax
  lda $dd00
  and #$fc
  ora #$02
  sta $dd00
  stx $d011

  asl $d019
  lda #$00
  sta $d015
  lda #$85  ;screen at $6000, spr data at $63f8
  sta $d018
  lda #$d8
  sta $d016

  lda #$00
  sta $d021
  sta $d020

  lda music_on
  beq .nomuss
  jsr MUSIC_PLAY
  jmp .cont2
.nomuss:
  ldx #$a0
.loo2:
  dex
  bne .loo2
.cont2:

  lda #$3b
  sta $d011

;  lda #<firstirq2
;  sta $0314
;  lda #>firstirq2
;  sta $0315
;  lda #irq2_raster
;  sta $d012
;  inc loaderirq_cou

  pla
  tay
  pla
  tax
  pla
  rti


;############### PUCRUNCH decrunch

SHORT = 1 ;1  ; assume file is ok

LZPOS EQU zpbase    ; 2 ZeroPage temporaries
bitstr  EQU zpbase+2    ; 1 temporary (does not need to be ZP)

decrunch: subroutine
  ; Call with X = HI of packed data, Y = LO of packed data
  ; Returns exec address in X = HI and Y = LO
  ; Carry will be set for error, cleared for OK
  ; Setup read pointer
  sty INPOS55
  stx INPOS55+1
#endif

#if SHORT
  ldx #5
.222$  jsr getbytdec  ; skip 'p', 'u', endAddr HI&LO, leave starting escape in A
  dex
  bne .222$
#else
  jsr getbytdec  ; 'p'
  cmp #112
  beq 9$
  sec   ; error
  jmp eof552
9$  jsr getbytdec  ; 'u'
  cmp #117
  beq 8$
  sec   ; error
  jmp eof552
8$  jsr getbytdec  ; skip endAddr
  jsr getbytdec
  jsr getbytdec
#endif
  sta esc55+1 ; starting escape

  jsr getbytdec  ; read startAddr
  sta OUTPOS55
  jsr getbytdec
  sta OUTPOS55+1
  jsr getbytdec  ; read # of escape bits
  sta escB055+1
  sta escB155+1
  lda #8
  sec
  sbc escB155+1
  sta noesc55+1 ; 8-escBits

  jsr getbytdec
  sta mg55+1  ; maxGamma + 1
  lda #9
  sec
  sbc mg55+1  ; 8 - maxGamma == (8 + 1) - (maxGamma + 1)
  sta longrle55+1
  jsr getbytdec
  sta mg155+1 ; (1<<maxGamma)
  asl
  clc
  sbc #0
  sta mg2155+1  ; (2<<maxGamma) - 1
  jsr getbytdec
  sta elzpb55+1

  jsr getbytdec  ; exec address
  sta lo+1  ; lo
  jsr getbytdec
  sta hi+1  ; hi

  jsr getbytdec  ; rleUsed
  ldx #0
  tay
0$  beq 1$    ; Y == 0 ?
  jsr getbytdec
  sta table,x
  inx
  dey
  bne 0$
1$  ; setup bit store - $80 means empty
  lda #$80
  sta bitstr
  jmp main55

getbytdec  jsr getnew55
  lda bitstr
  ror
  rts


newesc55  ldy esc55+1 ; remember the old code (top bits for escaped byte)
escB055 ldx #2    ; ** PARAMETER  0..8
  jsr getchk55f ; get & save the new escape code
  sta esc55+1
  tya   ; pre-set the bits
  ; Fall through and get the rest of the bits.
noesc55 ldx #6    ; ** PARAMETER  8..0
  jsr getchk55f
  jsr putch55 ; output the escaped/normal byte
  ; Fall through and check the escape bits again
main55  ldy #0    ; Reset to a defined state
  tya   ; A = 0
escB155 ldx #2    ; ** PARAMETER  0..8
  jsr getchk55f ; X = 0
esc55 cmp #0
  bne noesc55
  ; Fall through to packed code

  jsr getval55  ; X = 0
  sta LZPOS ; xstore - save the length for a later time
  lsr   ; cmp #1  ; LEN == 2 ? (A is never 0)
  bne lz7755  ; LEN != 2  -> LZ77
  ;tya    ; A = 0
  jsr get1bit55 ; X = 0
  lsr   ; bit -> C, A = 0
  bcc lz7755_2  ; A=0 -> LZPOS+1
  ;***FALL THRU***

  ; e..e01
  jsr get1bit55 ; X = 0
  lsr   ; bit -> C, A = 0
  bcc newesc55  ; e..e010
  ;***FALL THRU***

  ; e..e011
srle55  iny   ; Y is 1 bigger than MSB loops
  jsr getval55  ; Y is 1, get len, X = 0
  sta LZPOS ; xstore - Save length LSB
mg155 cmp #64   ; ** PARAMETER 63-64 -> C clear, 64-64 -> C set..
  bcc chrcode55 ; short RLE, get bytecode

longrle55 ldx #2    ; ** PARAMETER  111111xxxxxx
  jsr getbits55 ; get 3/2/1 more bits to get a full byte, X = 0
  sta LZPOS ; xstore - Save length LSB

  jsr getval55  ; length MSB, X = 0
  tay   ; Y is 1 bigger than MSB loops

chrcode55 jsr getval55  ; Byte Code, X = 0
  tax   ; this is executed most of the time anyway
  lda table-1,x ; Saves one jump if done here (loses one txa)

  cpx #32   ; 31-32 -> C clear, 32-32 -> C set..
  bcc 1$    ; 1..31, we got the right byte from the table

  ; Ranks 32..64 (11111°xxxxx), get byte..
  txa   ; get back the value (5 valid bits)
  ldx #3
  jsr getbits55 ; get 3 more bits to get a full byte, X = 0

1$  ldx LZPOS ; xstore - get length LSB
  inx   ; adjust for cpx#$ff;bne -> bne
dorle55 jsr putch55
  dex
  bne dorle55 ; xstore 0..255 -> 1..256
  dey
  bne dorle55 ; Y was 1 bigger than wanted originally
main55beq beq main55  ; reverse condition -> jump always


lz7755  jsr getval55  ; X = 0
mg2155  cmp #127  ; ** PARAMETER  Clears carry (is maximum value)
  bne noeof55
  ; EOF
eof55
#if SHORT == 0
  ; EOF
eof55 clc
eof552  lda #$37
  sta 1
  cli
#endif
hi  ldx #0
lo  ldy #0
  rts


noeof55 sbc #0    ; C is clear -> subtract 1  (1..126 -> 0..125)
elzpb55 ldx #0    ; ** PARAMETER (more bits to get)
  jsr getchk55f ; clears Carry, X = 0

lz7755_2  sta LZPOS+1 ; offset MSB
  jsr getbytdece ; clears Carry, X = 0
  ; Note: Already eor:ed in the compressor..
  ;eor #255 ; offset LSB 2's complement -1 (i.e. -X = ~X+1)
  adc OUTPOS55  ; -offset -1 + curpos (C is clear)
  ldx LZPOS ; xstore = LZLEN (read before it's overwritten)
  sta LZPOS

  lda OUTPOS55+1
  sbc LZPOS+1 ; takes C into account
  sta LZPOS+1 ; copy X+1 number of chars from LZPOS to OUTPOS55
  ;ldy #0   ; Y was 0 originally, we don't change it

  inx   ; adjust for cpx#$ff;bne -> bne
lzloop55  lda (LZPOS),y ; using abs,y is 3 bytes longer, only 1 cycle/byte faster
  jsr putch55 ; Note: must be copied forwards!
  iny   ; Y does not wrap because X=0..255 and Y initially 0
  dex
  bne lzloop55  ; X loops, (256,1..255)
  beq main55beq ; jump through another beq (-1 byte, +3 cycles)


getnew55  pha   ; 1 Byte/3 cycles

INPOS55 = *+1
  lda $aaaa ; ** PARAMETER
  inc INPOS55
  bne 0$
  inc INPOS55+1
#endif
0$  sec
  rol   ; Shift out the next bit and
      ;  shift in C=1 (last bit marker)
  sta bitstr  ; bitstr initial value = $80 == empty
  pla   ; 1 Byte/4 cycles
  rts
  ; 25+12 = 37

; getval55 : Gets a 'static huffman coded' value
; ** Scratches X, returns the value in A **
getval55  inx   ; X <- 1
  txa   ; set the top bit (value is 1..255)
gv055 asl bitstr
  bne 1$
  jsr getnew55
1$  bcc getchk55  ; got 0-bit
  inx
mg55  cpx #7    ; ** PARAMETER unary code maximum length + 1
  bne gv055
  beq getchk55  ; inverse condition -> jump always
  ; getval55: 18 bytes
  ; 15 + 17*n + 6+15*n+12 + 36*n/8 = 33 + 32*n + 36*n/8 cycles

; getbits55: Gets X bits from the stream
; ** Scratches X, returns the value in A **
getbytdece ldx #7
get1bit55 inx   ;2
getbits55 asl bitstr
  bne 1$
  jsr getnew55
1$  rol   ;2
getchk55  dex   ;2    more bits to get ?
getchk55f bne getbits55 ;2/3
  clc   ;2    return carry cleared
  rts   ;6+6


OUTPOS55 = *+1    ; ZP
putch55 sta $aaaa ; ** parameter
  inc OUTPOS55  ; ZP
  bne 0$
  inc OUTPOS55+1  ; ZP
0$  rts



table EQU depackbuffer





code_end:

 seg sinus
 org $1ec0
;If you change the org, change it in globals.s as well...
sintbl2:        dc.b 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45
                dc.b 48,51,54,57,59,62,65,67,70,73,75,78,80,82,85,87
                dc.b 89,91,94,96,98,100,102,103,105,107,108,110,112,113,114,116
                dc.b 117,118,119,120,121,122,123,123,124,125,125,126,126,126,126,126
costbl2:        dc.b 126,126,126,126,126,126,125,125,124,123,123,122,121,120,119,118
                dc.b 117,116,114,113,112,110,108,107,105,103,102,100,98,96,94,91
                dc.b 89,87,85,82,80,78,75,73,70,67,65,62,59,57,54,51
                dc.b 48,45,42,39,36,33,30,27,24,21,18,15,12,9,6,3
                dc.b 0,-3,-6,-9,-12,-15,-18,-21,-24,-27,-30,-33,-36,-39,-42,-45
                dc.b -48,-51,-54,-57,-59,-62,-65,-67,-70,-73,-75,-78,-80,-82,-85,-87
                dc.b -89,-91,-94,-96,-98,-100,-102,-103,-105,-107,-108,-110,-112,-113,-114,-116
                dc.b -117,-118,-119,-120,-121,-122,-123,-123,-124,-125,-125,-126,-126,-126,-126,-126
                dc.b -126,-126,-126,-126,-126,-126,-125,-125,-124,-123,-123,-122,-121,-120,-119,-118
                dc.b -117,-116,-114,-113,-112,-110,-108,-107,-105,-103,-102,-100,-98,-96,-94,-91
                dc.b -89,-87,-85,-82,-80,-78,-75,-73,-70,-67,-65,-62,-59,-57,-54,-51
                dc.b -48,-45,-42,-39,-36,-33,-30,-27,-24,-21,-18,-15,-12,-9,-6,-3
                dc.b 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45
                dc.b 48,51,54,57,59,62,65,67,70,73,75,78,80,82,85,87
                dc.b 89,91,94,96,98,100,102,103,105,107,108,110,112,113,114,116
                dc.b 117,118,119,120,121,122,123,123,124,125,125,126,126,126,126,126

  seg music
  org $2000
;If you change the org, change it in globals.s as well...
; incbin Music/exit.raw
 incbin Music/Klanen_Manne.raw
;Max music size = $13ff

  seg code
;  org $3180
  org $3400

exitsprite0:
 dc.b $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
 dc.b $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
 dc.b $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
 dc.b $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
 dc.b $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
 dc.b $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
 dc.b $ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff
 dc.b $ff,$ff,$ff,$ff,$ff,$ff,$ff,$00

exitsprite1:
 dc.b %00000000,%00000000,%00000000
 dc.b %00000000,%00000000,%00000000
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111000,%00000000
 dc.b %00111111,%11100000,%00000000
 dc.b %00111111,%11000000,%00000000
 dc.b %00111111,%10000000,%01111111
 dc.b %00111111,%00000001,%11111110
 dc.b %00111110,%00000111,%11111100
 dc.b %00111100,%00011111,%11111000
 dc.b %00111100,%01111111,%11110000,0

exitsprite2:
 dc.b %00000000,%00000000,%00000000
 dc.b %00000000,%00000000,%00000000
 dc.b %11111111,%11111111,%11111111
 dc.b %11111111,%11111111,%11111111
 dc.b %11111111,%11111111,%11111111
 dc.b %11111100,%00011111,%11111111
 dc.b %11100000,%00000011,%11111111
 dc.b %11000000,%00000001,%11111111
 dc.b %11000000,%00000001,%11111111
 dc.b %11000000,%00000011,%11111111
 dc.b %11000000,%00001111,%11111111
 dc.b %11100000,%00111111,%11111111
 dc.b %11110000,%01111111,%11111111
 dc.b %00000000,%00111111,%11111111
 dc.b %00000000,%00000111,%11111111
 dc.b %00000000,%00000001,%11111111
 dc.b %00000000,%00000000,%01111111
 dc.b %00000000,%00000000,%00000000
 dc.b %00000000,%00011000,%00000000
 dc.b %00000000,%00111110,%00000000
 dc.b %00000000,%01111111,%11111111,0

exitsprite3:
 dc.b %00111111,%11111111,%11100000
 dc.b %00111111,%11111111,%11000000
 dc.b %00111111,%11111111,%10000000
 dc.b %00111111,%11111111,%00000000
 dc.b %00111111,%11111110,%00000000
 dc.b %00111111,%11111100,%00000000
 dc.b %00111111,%11111100,%00000000
 dc.b %00111111,%11111000,%00000111
 dc.b %00111111,%11111000,%00000111
 dc.b %00111111,%11111000,%00000111
 dc.b %00100000,%00000000,%00000111
 dc.b %01000000,%00000000,%00000111
 dc.b %10000000,%00000000,%00001111
 dc.b %11111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %00111111,%11111111,%11111111
 dc.b %01111111,%11111111,%11111111
 dc.b %11111111,%11111111,%11111111
 dc.b %00000000,%00000000,%00000000
 dc.b %00000000,%00000000,%00000000,0

exitsprite4:
 dc.b %00000000,%11111111,%11111111
 dc.b %00000001,%11111111,%11111111
 dc.b %00000011,%11111111,%11111111
 dc.b %00000111,%11111111,%11111111
 dc.b %00001111,%11111111,%11111111
 dc.b %00000111,%11111111,%11111111
 dc.b %00000011,%11111111,%11111111
 dc.b %00000001,%11111111,%11111111
 dc.b %10000000,%11111111,%11111111
 dc.b %11000000,%01111111,%11111111
 dc.b %11100000,%00111111,%11111111
 dc.b %11110000,%00011111,%11111111
 dc.b %11111000,%00001111,%11111111
 dc.b %11111100,%00000111,%11111111
 dc.b %11111110,%00000001,%11111111
 dc.b %11111111,%00000000,%11111111
 dc.b %11111111,%11111111,%11111100
 dc.b %11111110,%00000111,%11110000
 dc.b %11111100,%00011111,%11000000
 dc.b %00000000,%00000000,%00000000
 dc.b %00000000,%00000000,%00000000,0

exitsprite5:
 dc.b %11111111,%11111010,%00000000
 dc.b %10000000,%00000001,%00000000
 dc.b %10000000,%00000000,%10000000
 dc.b %10000000,%00000000,%01000000
 dc.b %10000000,%00000000,%00100000
 dc.b %10000000,%00000000,%00010000
 dc.b %10000000,%00000000,%00001000
 dc.b %10000000,%00000000,%00000100
 dc.b %10000000,%00000000,%00000010
 dc.b %10000000,%00000000,%00000001
 dc.b %11111111,%00000000,%00000000
 dc.b %10000000,%00000000,%00000001
 dc.b %10000000,%00000000,%00000010
 dc.b %10000000,%00000000,%00000100
 dc.b %10000000,%00000000,%00001000
 dc.b %10000000,%00000000,%00010000
 dc.b %10000000,%00000000,%00100000
 dc.b %10000000,%00000000,%01000000
 dc.b %10000000,%00000000,%10000000
 dc.b %10000000,%00000001,%00000000
 dc.b %11111111,%11111010,%00000000,0

exitsprite6:
 dc.b %00000000,%00101011,%11111111
 dc.b %00000000,%01001000,%00100000
 dc.b %00000000,%10001000,%00100000
 dc.b %00000001,%00001000,%00100000
 dc.b %00000010,%00001000,%00100000
 dc.b %00000100,%00001000,%00100000
 dc.b %00001000,%00001000,%00100000
 dc.b %00010000,%00001000,%00100000
 dc.b %00100000,%00001000,%00100000
 dc.b %01000000,%00001000,%00100000
 dc.b %10000000,%00001000,%00100000
 dc.b %01000000,%00001000,%00100000
 dc.b %00100000,%00001000,%00100000
 dc.b %00010000,%00001000,%00100000
 dc.b %00001000,%00001000,%00100000
 dc.b %00000100,%00001000,%00100000
 dc.b %00000010,%00001000,%00100000
 dc.b %00000001,%00001000,%00100000
 dc.b %00000000,%10001000,%00100000
 dc.b %00000000,%01001000,%00100000
 dc.b %00000000,%00101000,%00100000,0



; org $3ffe
; incbin "sign01.ish"
  org $4000
  incbin "jsl_logo_defiers2_bitmap.raw"
  org $6000
  incbin "jsl_logo_defiers2_screen.raw"
  org $6400
  incbin "jsl_logo_defiers2_colours.raw"


; org image_packed-2
; incbin "sign01.puc"

