
//#resource "vcs-ca65.h"

system Init
  on main_init do once
---
.include "vcs-ca65.h"
.macpack longbranch
.define PAL 0
.segment "STARTUP"
__NMI:
__Reset:
__BRK:
    CLEAN_START
{{!start}}	; start main routine
.segment "VECTORS"
ZeroByte:  .byte $00
Return:    .byte $60
VecNMI:
VecReset:  .word __Reset
VecBRK:    .word __BRK
.code
---
end

component Player
end

component KernelSection
  lines: 1..255 default 1
end

component BGColor
  bgcolor: 0..255
end

component FGColor
  fgcolor: 0..255
end

component PFColor
  pfcolor: 0..255
end

component Playfield
   pf: 0..0xffffff
end

component AsymPlayfield
   pfleft: 0..0xffffff
   pfright: 0..0xffffff
end

system FrameLoop
  on start do once 
---
    {{emit preframeloop}}
@NextFrame:
    FRAME_END
    {{emit prevsync}}
    FRAME_START
    {{emit preframe}}
    {{emit prekernel}}
    KERNEL_START
    {{emit kernel}}
    KERNEL_END
    {{emit postkernel}}
    {{emit postframe}}
    jmp @NextFrame    ; loop to next frame
---
    on postframe do once
---
    lsr SWCHB	      ; test Game Reset switch
    bcs @NoStart
    {{emit resetswitch}}
@NoStart:
---
end

system ResetConsole
    on resetswitch do once
---
    jmp Main::__Reset ; jump to Reset handler
---
end

system JoyButton
    on postframe do foreach [Player]
---
    lda {{index INPT4}}	;read button input
    bmi @NotPressed
    {{emit joybutton}}
@NotPressed:
---
end

system Joystick
    locals 1
    on postframe do once
---
; 2 control inputs share a single byte, 4 bits each
    lda SWCHA
    sta {{$0}}
---
    on postframe do foreach [Player] limit 2
---
    asl {{$0}}
.ifdef EVENT__joyright
    bcs @SkipMoveRight
    {{!joyright}}
@SkipMoveRight:
.endif
    asl {{$0}}
.ifdef EVENT__joyleft
    bcs @SkipMoveLeft
    {{!joyleft}}
@SkipMoveLeft:
.endif
    asl {{$0}}
.ifdef EVENT__joydown
    bcs @SkipMoveDown
    {{!joydown}}
@SkipMoveDown:
.endif
    asl {{$0}}
.ifdef EVENT__joyup
    bcs @SkipMoveUp
    {{!joyup}}
@SkipMoveUp:
.endif
---
end

system SetHorizPos
    on SetHorizPos do critical fit 22 once 
---
; SetHorizPos routine
; A = X coordinate
; Y = player number (0 or 1)
    sec		    	; set carry flag
    sta WSYNC		; start a new line
:
    sbc #15		; subtract 15
    bcs :-		; branch until negative
    eor #7		; calculate fine offset
    asl
    asl
    asl
    asl
    sta HMP0,y	; set fine offset
    sta RESP0,y	; fix coarse position
    sta WSYNC   ; won't overrun if X < 150
---
end


system StandardKernel
  on wsync do once
---
    sta WSYNC
---
  on preframe do foreach [KernelSection] limit 1
---
    {{!wsync}}
    {{!kernelsetup}}
---
  on kernel do foreach [KernelSection]
---
    {{!wsync}}
    {{!kernelsetup}}
    {{!kerneldraw}}
    {{!kerneldone}}
---
  on kerneldraw do with [KernelSection]
---
    ldy {{<lines}}
@loop:
    {{!prescanline}}
    {{!wsync}}
    {{!scanline}}
    dey
    bne @loop
---
  on kernelsetup do if [BGColor]
---
    lda {{<bgcolor}}
    sta COLUBK
---
  on kernelsetup do if [PFColor]
---
    lda {{get pfcolor}}
    sta COLUPF
---
  on kernelsetup do if [Playfield]
---
    lda {{get pf 0}}
    sta PF0
    lda {{get pf 8}}
    sta PF1
    lda {{get pf 16}}
    sta PF2
---
end

component SimpleCollidable
  lastx: 0..255
  lasty: 0..255
end

system SimpleCollision
  on postframe do with [SimpleCollidable]
  ---
  lda CXP0FB
  bpl @nocollide
  lda {{<lastx}}
  sta {{<xpos}}
  lda {{<lasty}}
  sta {{<ypos}}
@nocollide:
  ---
  on ymoved do with [SimpleCollidable]
  ---
  lda {{<xpos}}
  sta {{<lastx}}
  lda {{<ypos}}
  sta {{<lasty}}
  ---
end

component FrameCount
  frame: 0..255
end

system FrameCounter
  on postframe do with [FrameCount]
---
  inc {{set frame}}
.ifdef EVENT__frame256
  jne @noframe256
  {{emit frame256}}
@noframe256:
.endif
---
// TODO: only .if responds to events
   on postframe do with [FrameCount]
   ---
   jmp @go
.ifdef EVENT__frame4
@frame4:
   {{emit frame4}}
   jmp @done
.endif
.ifdef EVENT__frame8
@frame8:
   {{emit frame8}}
   jmp @done
.endif
.ifdef EVENT__frame16
@frame16:
   {{emit frame16}}
   jmp @done
.endif
@go:
   lda {{get frame}}
.ifdef EVENT__frame16
   and #15
   jeq @frame16
.endif
.ifdef EVENT__frame8
   and #7
   cmp #3
   jeq @frame8
.endif
.ifdef EVENT__frame4
   and #3
   cmp #1
   jeq @frame4
.endif
@done:
   ---
end



///

demo Main
  using FrameLoop, ResetConsole
  using StandardKernel, JoyButton
  
  entity [Player]
  end
  entity [KernelSection,BGColor]
    const lines = 2
    const bgcolor = $1a
  end
  entity [KernelSection,BGColor]
    const lines = 2
    const bgcolor = $16
  end
  entity [KernelSection,BGColor]
    const lines = 2
    const bgcolor = $14
  end
  entity [KernelSection,BGColor]
    const lines = 2
    const bgcolor = $12
  end
  entity  [KernelSection,BGColor,Playfield]
    const lines = 10
    const bgcolor = $14
    const pf = 0x125244
  end
  entity Trees [KernelSection,BGColor,PFColor,Playfield]
    const lines = 50
    const bgcolor = $14
    const pf = 0x112244
  end
  entity Trees2 [KernelSection,BGColor,PFColor,Playfield]
    const lines = 50
    const bgcolor = $16
    const pf = 0x124
  end
  entity [KernelSection,BGColor,Playfield]
    const lines = 10
    const bgcolor = $18
    const pf = 0
  end
  
  system Local
    on joybutton do with [#Trees] ---
    inc {{set pfcolor}}
    ---
  end
  
end demo

