;### Sprite-plot-routine on the right

                 processor 6502

;Memory map:
;$80-$ff Zero page

;$7000-$85ff Code+charset

;$8000-$83ff Screen0
;$8400-$87ff Screen1
;$8800-$8fff Charset
;$a000-$a07f doublebuffered sprites to draw in

;screen_sprite_pointer0 = $83f8
screen_sprite_pointer0 = $63f8
screen_sprite_pointer1 = $87f8


  include globals.s

  seg.u zp
  org $0080
zp_start:

savea: dc.b 0
savex: dc.b 0
savey: dc.b 0
nmi_savea: dc.b 0
nmi_savex: dc.b 0
nmi_savey: dc.b 0

part_finished: dc.b 0
IRQ_finished: dc.b 0
countdown_to_finished: dc.b 0

up_toggle:             dc.b 0  ;Sr en variabel som fsr scrollen at gs varannat svep.
up_current_row:        dc.b 0  ;verkar vara radnummer som vi skriver in ny text ps. 7158 Sndrar $03 (÷kar den med 1), wrap and $0f
up_current_rowpixel:   dc.b 0  ;verkar vara pixelnummer i en rad. 714d minskar den med 1, laddar om med #$14
up_irq_counter:        dc.b 0
up_sprites_ypos:       dc.b 0  ;har med sprite y-position att g÷ra
up_current_displayrow: dc.b 0  ;har med sprite data att g÷ra. ldx $07. lda $72a0,x. 712a ÷kar $07 wrap and #$0f
up_textpoi:            dc.b 0,0 ;Pointer till text i upscreen
up_thisrow_sprx:       dc.b 0   ;SStts till #$40, x-pos f÷r en rad vi ritar i
up_current_xdraw:      dc.b 0   ;pixelpekare ps var vi Sr i raden nSr vi ritar
up_underline:          dc.b 0   ;#$80 = understrykning on
up_drawpoi:            dc.b 0,0 ;SStts till #0,($f * 2) + #$40 = Pekare ps de sprites vi ska rita i
up_draw_sprline:       dc.b 0   ;sStts till ($03 + #$e) and #$f. Troligtvis rad vi ritar ut ny text ps.
up_tmp:                dc.b 0   ;tempvariabel

countdown:             dc.b 0,0 ;How long before exit

up_sincou:             dc.b 0
up_cou:                dc.b 0
up_cou2:                dc.b 0

up_pause: dc.b 0

  seg code
  org $7000

;###
;### up-scroller
;###

  jmp start_up
the_sprite:  
  org $7100
  ds.b $40,0

start_up:
  ldx #0
.lok:  
  lda $6000,x
  eor #$11
  sta $6000,x
  lda $6100,x
  eor #$11
  sta $6100,x
  lda $6200,x
  eor #$11
  sta $6200,x
  inx
  bne .lok  
.lok2:  
  lda $6300,x
  eor #$11
  sta $6300,x
  inx
  cpx #$f8
  bne .lok2  
  
  ldx #0
  lda #$b
.coll:
  sta $d800,x
  sta $d900,x
  sta $da00,x
  sta $daf7,x
  inx
  bne .coll

  ldx #0
  lda #$0
.scr0:
  sta $8000,x
  sta $8100,x
  sta $8200,x
  sta $82f7,x
  inx
  bne .scr0

  ldx #0
  lda #$1
.scr1:
  sta $8400,x
  sta $8500,x
  sta $8600,x
  sta $86f7,x
  inx
  bne .scr1

  jsr spr_init
  
  sei
  lda #$35
  sta $01
  lda #$7f
  sta $dc0d
  lda $dc0d
  ldx #$2e
.d000loop:
  lda up_d000defaults,x
  sta $d000,x
  dex
  bpl .d000loop
  inx
  stx up_irq_counter
  lda #%00000010 ;$4000-$7fff
;  lda #%00000001
  sta $dd00   ;Bank at $8000-$bfff

  lda #$00
  sta up_current_row
  lda #$14
  sta up_current_rowpixel
  sei
  lda #<up_irq1
  sta $fffe
  lda #>up_irq1
  sta $ffff
  lda #$fc   ;set nmi to rti
  sta $fffa
  lda #$ff
  sta $fffb
  lda #$40   ;rti
  sta $fffc
  lda #<up_text
  sta up_textpoi
  lda #>up_text
  sta up_textpoi+1

;$80 = $6000
;$a0 = $6800

  ldx #$c4
  stx screen_sprite_pointer0
  stx screen_sprite_pointer0+1
  stx screen_sprite_pointer0+2
  stx screen_sprite_pointer0+3
  stx screen_sprite_pointer0+4
  stx screen_sprite_pointer0+5
  stx screen_sprite_pointer0+6
  stx screen_sprite_pointer0+7
  ldx #$80
  stx screen_sprite_pointer1
  stx screen_sprite_pointer1+1
  stx screen_sprite_pointer1+2
  stx screen_sprite_pointer1+3
  stx screen_sprite_pointer1+4
  stx screen_sprite_pointer1+5
  stx screen_sprite_pointer1+6
  stx screen_sprite_pointer1+7

  lda #$2
  sta countdown+1
  lda #$80
  sta countdown

  lda #$40
  sta up_thisrow_sprx
  lda #$0
  sta up_current_xdraw
  sta up_underline
  sta up_sincou
  sta up_cou
  sta up_cou2
  sta up_pause
  jsr up_move

  lda #$98
  ldx #0
  sta $d000
  adc #$18
  sta $d002
  adc #$18
  sta $d004
  adc #$18
  sta $d006
  adc #$18
  sta $d008
  adc #$18
  sta $d00a
  bcc .no_3msb
  ldx #$e0
  stx $d010
  clc
.no_3msb:
  adc #$18
  sta $d00c
  bcc .no_2msb
  ldx #$c0
  stx $d010
  clc
.no_2msb:
  adc #$18
  sta $d00e
  bcc .no_1msb
  ldx #$80
.no_1msb:
  stx $d010

  lda #$01
  sta $d019
  cli

up_ever:
  lda up_toggle
.nonew:
  cmp up_toggle
  beq .nonew

  dec countdown
  bne .no_end_part
  dec countdown+1
  bne .no_end_part
  
;ToDo - remove this  
;  jmp up_ever

up_end_part:
  lda #0
  sta $dc0e ;stop timers
  lda #$7f
  sta $dc0d
  lda $dc0d
;  jmp next_demo_part
  jsr MUSIC_PLAY
  rts

.no_end_part:
;Ok, now draw a new frame in the charset indicated by bit0 of up_toggle
;Ok, now draw a new frame in the sprite indicated by bit0 of up_toggle
  jsr spr_rut
  jmp up_ever


up_move:
  inc up_cou
  bne .hh
  inc up_cou+1
.hh
  lda up_cou2
  and #1
  bne .g1
  lda up_cou
  eor #$ff
  tax
  lda costbl,x
  cmp #$80
  ror
  sta up_sincou
.g1:
  rts

up_irq1:
  bit $dc0d
  bmi .noirq2
  jmp up_irq2
.noirq2:
  cli
  pha
  txa
  pha
  lda $d001
  clc
  adc #$15
  ldx up_sprites_ypos
  bne .sety
  cmp #$24
  bcc .sety
  sbc #$38
.sety:
  sta $d001
  sta $d003
  sta $d005
  sta $d007
  sta $d009
  sta $d00b
  sta $d00d
  sta $d00f
;set spritepointers
;  lda up_current_displayrow
;  asl
;  asl
;  asl
;  clc
;  adc #$80
;  tax
;  stx screen_sprite_pointers
;  inx
;  stx screen_sprite_pointers+1
;  inx
;  stx screen_sprite_pointers+2
;  inx
;  stx screen_sprite_pointers+3
;  inx
;  stx screen_sprite_pointers+4
;  inx
;  stx screen_sprite_pointers+5
;  inx
;  stx screen_sprite_pointers+6
;  inx
;  stx screen_sprite_pointers+7
;setx:

;next:
  ldx up_current_displayrow
  inx
  cpx #$f
  bne .nowra
  ldx #0
.nowra:
  stx up_current_displayrow
  ldx up_sprites_ypos
  inx
  stx up_sprites_ypos
  cpx #$0d
  bne .no_new_row

  lda #$7f
  sta $dc0d
  lda $dc0d

;Scroll only every other screen:
  inc up_toggle
  lda up_toggle
  lsr
  bcc .no_new_row

.up_running:
  ldx up_current_rowpixel
;  dex
  stx up_current_rowpixel
  bpl .no_new_row
  ldx #$14
  stx up_current_rowpixel
  ldx up_current_row
  inx
  cpx #$0f
  bne .nowrb
  ldx #0
.nowrb:
  stx up_current_row

.no_new_row:
;  lda up_toggle
;  and #$01
;  asl
;  asl
;  asl
;  asl
;  lda #$02  ;Charset at $8800-, screen $8000 or $8400
  lda #$85  ;screen $6000
  sta $d018

  lda up_sprites_ypos
  cmp #$01
  bne .no_music
;  cmp #$04
;  beq .music
;  cmp #$07
;  beq .music
;  cmp #$0a
;  bne .no_music
.music:
  tya
  pha
;  nop
;  nop
;  nop
;  jsr up_move
  jsr MUSIC_PLAY

  pla
  tay
.no_music:
  pla
  tax
  pla
  rti

up_irq2:
  pha
  lda up_irq_counter
  bne up_irq3
;  lda #$90
  lda #$b3
  sta $d011
  lda #$1f
  sta $d012
  lda #$01
  sta up_irq_counter
  sta $d019
  pla
  rti

up_irq3:
  cmp #$01
  bne up_irq4
  lda #$26
  sta $d012
  inc up_irq_counter
  lda #$01
  sta $d019
  nop
  nop
  nop
  lda #$ff
  sta $bfff
  pla
  rti

up_irq4:
  cmp #$02
  beq .up_irq6
  jmp up_irq5

.up_irq6:
  lda #$3b
  sta $d011
  lda up_current_rowpixel
  sec
  sbc #$06
  bcs .h
  adc #$38
.h:
  sta $d001
  sta $d003
  sta $d005
  sta $d007
  sta $d009
  sta $d00b
  sta $d00d
  sta $d00f
  txa
  pha

  lda up_current_rowpixel
  sec
  sbc #$0b
  bcs .k
  adc #$38
.k:
  ldx #$2a
  stx $dc04
  ldx #$05
  stx $dc05
  ldx #$91
.wait_raster:
  cmp $d012
  bne .wait_raster
  stx $dc0e

  lda #$81
  sta $dc0d
  lda $dc0d
  lda #$00
  sta up_sprites_ypos
  ldx up_current_row
  inx
  cpx #$0f
  bne .nowrc
  ldx #$00
.nowrc:
  stx up_current_displayrow
  lda #$0e
  sta $d012
  inc up_irq_counter
  lda #$01
  sta $d019
  pla
  tax
  pla
  rti

up_irq5:
  lda #$f8
  sta $d012
  lda #$00
  sta up_irq_counter
  lda #$01
  sta $d019
  lda #$00
  sta $bfff
  pla
  rti

;###
;###
;###


;72af-72de defaultvärden för $d000-$d02e
up_d000defaults:
 dc.b $00, $00, $00, $00, $00, $00, $00, $00
 dc.b $00, $00, $00, $00, $00, $00, $00, $00
 dc.b $00, $1b, $f8, $00, $00, $ff, $c8, $00
 dc.b $02, $01, $01, $00, $00, $00, $00, $00
 dc.b $00, $00, $00, $00, $00, $00, $00, $01
 dc.b $01, $01, $01, $01, $01, $01, $01

;###
;### Spriteplot
;###

which: dc.b 0
wcoux: dc.b 0
wcouy: dc.b 0
tmp2: dc.b 0
x_start: dc.b 0
points: dc.b 0

spr_init:
  ldx #0
.cmo:  
  lda sintbl,x
  clc
  adc #$80
  lsr
  lsr
  lsr
  sta sintbl2,x
  
  lda costbl,x
  clc
  adc #$80
  lsr
  lsr
  sta costbl2,x
  inx
  bne .cmo
  rts

spr_rut:
  lda #0  
  ldy #0
.clr:  
  sta $7100,y
;  sta $a000,y
  iny
  cpy #$3f
  bne .clr
  inc wcoux
  dec wcouy
  dec wcouy
  
  

  lda #$50
  sta points  
  lda wcoux
  sta wcouxtmp+1
  lda wcouy
  sta wcouytmp+1
  
.morr:  
;calc_y:
wcouytmp:
  ldx #0
  ldy sintbl2,x
  inx
  inx
  inx
  stx wcouytmp+1
  lda ytbl,y
  tay

wcouxtmp:  
;  ldx #0
  lda costbl2
  inc wcouxtmp+1
  tax

  lda xbigtbl,x
  sta modx+1
  sta modx2+1
  lda xtbl,x
modx:  
  ora $7100,y
modx2:  
  sta $7100,y

  dec points
  bne .morr

  rts

xtbl:
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
  dc.b $80,$40,$20,$10,$08,$04,$02,$01
xbigtbl:
  dc.b 0,0,0,0,0,0,0,0
  dc.b 1,1,1,1,1,1,1,1
  dc.b 2,2,2,2,2,2,2,2  
  dc.b 0,0,0,0,0,0,0,0
  dc.b 1,1,1,1,1,1,1,1
  dc.b 2,2,2,2,2,2,2,2  
  dc.b 0,0,0,0,0,0,0,0
  dc.b 1,1,1,1,1,1,1,1
  dc.b 2,2,2,2,2,2,2,2  
  dc.b 0,0,0,0,0,0,0,0
  dc.b 1,1,1,1,1,1,1,1
  dc.b 2,2,2,2,2,2,2,2  
  dc.b 0,0,0,0,0,0,0,0
  dc.b 1,1,1,1,1,1,1,1
  dc.b 2,2,2,2,2,2,2,2  
  dc.b 0,0,0,0,0,0,0,0

ytbl:
  dc.b 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60
  dc.b 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60
  dc.b 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60
  dc.b 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60
  dc.b 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60
  dc.b 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60
  dc.b 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60

  align $100
sintbl2: ds.b $100,0
costbl2: ds.b $100,0
sintbl3: ds.b $100,0
costbl3: ds.b $100,0


;###
;### up-text
;###

up_text:
 dc.b " ",$fe
 dc.b " ",$fe
 dc.b " ",$fe
 dc.b " ",$fe
 dc.b " ",$fe

 dc.b $20,$ff

 org $8800
 dc.b %00000000
 dc.b %00000000
 dc.b %00000000
 dc.b %00000000
 dc.b %00000000
 dc.b %00000000
 dc.b %00000000
 dc.b %11111111

 dc.b %00000001
 dc.b %00000001
 dc.b %00000001
 dc.b %00000001
 dc.b %00000001
 dc.b %00000001
 dc.b %00000001
 dc.b %00000001


 org $a000
  dc.b %00000000,%00000000,%00000000
  dc.b %00000000,%11111111,%00000000
  dc.b %00001111,%11111111,%11110000
  dc.b %00111111,%11111111,%11111100
  dc.b %01111111,%11111111,%11111100
  dc.b %01111111,%11111111,%11111110
  dc.b %11111111,%11111111,%11111100
  dc.b %11111111,%10000001,%11111000
  dc.b %11111111,%00000000,%11100000
  dc.b %11111111,%00000000,%00000000
  dc.b %11111111,%00000000,%00000000
  dc.b %11111111,%00011111,%11100000
  dc.b %11111111,%00111111,%11111100
  dc.b %11111111,%00010000,%11111100
  dc.b %11111111,%00000000,%11111110
  dc.b %11111111,%00000000,%11111110
  dc.b %11111111,%00000000,%11111110
  dc.b %11111111,%10000001,%11111110
  dc.b %01111111,%11111111,%11111100
  dc.b %00011111,%11111111,%11110000
  dc.b %00000000,%00000000,%00000000,0

  dc.b %00000000,%00000000,%00000000
  dc.b %00000000,%11111111,%00000011
  dc.b %00001111,%11111111,%11110011
  dc.b %00111111,%11111111,%11111100
  dc.b %01111111,%11111111,%11111100
  dc.b %01111111,%11111111,%11111110
  dc.b %11111111,%11111111,%11111100
  dc.b %11111111,%10000001,%11111000
  dc.b %11111111,%00000000,%11100000
  dc.b %11111111,%00000000,%00000000
  dc.b %11111111,%00000000,%00000000
  dc.b %11111111,%00011111,%11100000
  dc.b %11111111,%00111111,%11111100
  dc.b %11111111,%00010000,%11111100
  dc.b %11111111,%00000000,%11111110
  dc.b %11111111,%00000000,%11111110
  dc.b %11111111,%00000000,%11111110
  dc.b %11111111,%10000001,%11111110
  dc.b %01111111,%11111111,%11111100
  dc.b %00011111,%11111111,%11110000
  dc.b %00000000,%00000000,%00000000,0


