# Kernel
# A Kernel draws a set of scanlines to the screen.

component Kernel
lines: 0..255     "Height of region in scanlines"
bgcolor: 0..255   "Background color"
end

system SimpleKernel
tempbytes 8
on preframe do once [Kernel] ---
  lda #192 ; TODO: numlines
  sec
  sbc ypos_ypos_b0+ent
  sta {{$5}}+ofs

  ldy hasbitmap_bitmap_b0+ent
  lda bitmap_bitmapdata_b0,y
  sec
  sbc {{$5}}+ofs
  sta {{$0}}+ofs
  lda bitmap_bitmapdata_b8,y
  sbc #0
  sta {{$1}}+ofs

  ldy hascolormap_colormap_b0+ent
  lda colormap_colormapdata_b0,y
  sec
  sbc {{$5}}+ofs
  sta {{$2}}+ofs
  lda colormap_colormapdata_b8,y
  sbc #0
  sta {{$3}}+ofs

  lda sprite_height_b0+ent
  sta {{$4}}+ofs
  lda ypos_ypos_b0+ent
  sta {{$5}}+ofs
---
on preframe do once [Sprite,HasBitmap,HasColormap,HasYpos] --
  {{@KernelSetup}} 0,0
  {{@KernelSetup}} 1,6
--
on kernel do once [Kernel]:
  lda %{<bgcolor}
  sta COLUBK
  ldy %{<lines}
@LVScan:
  lda %{$4} ; height
  dcp %{$5}
  bcs @DoDraw1
  lda #0
  .byte $2C
@DoDraw1:
  lda (%{$0}),y
  sta WSYNC
  sta GRP0
  lda (%{$2}),y
  sta COLUP0

  lda %{$10} ; height
  dcp %{$11}
  bcs @DoDraw2
  lda #0
  .byte $2C
@DoDraw2:
  lda (%{$6}),y
  sta GRP1
  lda (%{$8}),y
  sta COLUP1

  dey		; decrement
  bne @LVScan	; repeat until 192 lines
--
end

scope Root

entity kernel [SimpleKernel]
const lines = 100
end

entity player1 [Sprite,HasBitmap,HasColormap,HasYpos]
const plyrindex = 0
init height = 8
init xpos = 100
init ypos = 100
end

end scope
