!macro BasicStart .line_num {
*=$0801
  !byte <.next_line, >.next_line
  !byte <.line_num, >.line_num
  !byte $9e
  !byte $30 + (.code_start / 1000)
  !byte $30 + ((.code_start / 100) % 10)
  !byte $30 + ((.code_start / 10) % 10)
  !byte $30 + (.code_start % 10)
	!byte 0
.next_line
  !byte 0, 0
.code_start
}

!macro BasicStart .line_num, .del_count, .cr_count {
*=$0801
  !byte <.next_line, >.next_line
  !byte <.line_num, >.line_num
  !byte $9e
  !byte $30 + (.code_start / 1000)
  !byte $30 + ((.code_start / 100) % 10)
  !byte $30 + ((.code_start / 10) % 10)
  !byte $30 + (.code_start % 10)
  !byte $3a, $8f, $20
	!fill .del_count, $14
  !pet "(c) simon/cgtr"
  !fill .cr_count, $0d
	!byte 0
.next_line
  !byte 0, 0
.code_start
}

!macro FillBlock .addr, .char {
  ldx #$00
  lda #.char
-
  sta .addr + $0000, x
	sta .addr + $0100, x
	sta .addr + $0200, x
	sta .addr + $02e8, x
	inx
  bne -
}

!macro FillBlock .addr, .char, .count {
	ldx #$00
	lda #.char
-
	sta .addr, x
	inx
	cpx #.count
	bne -
}

!macro CopyBlock .addr, .data, .count {
	ldx #$00
-
  lda .data, x
	sta .addr, x
	inx
	cpx #.count
	bne -
}

!macro CopyBlock .addr1, .data1, .addr2, .data2, .count {
	ldx #$00
-
  lda .data1, x
	sta .addr1, x
  lda .data2, x
	sta .addr2, x
	inx
	cpx #.count
	bne -
}

!macro CopyBlock .addr1, .data1, .addr2, .data2, .addr3, .data3, .count {
	ldx #$00
-
  lda .data1, x
	sta .addr1, x
  lda .data2, x
	sta .addr2, x
  lda .data3, x
	sta .addr3, x
	inx
	cpx #.count
	bne -
}

!macro CopyBlock .addr1, .data1, .addr2, .data2, .addr3, .data3, .addr4, .data4, .count {
	ldx #$00
-
  lda .data1, x
	sta .addr1, x
  lda .data2, x
	sta .addr2, x
  lda .data3, x
	sta .addr3, x
  lda .data4, x
	sta .addr4, x
	inx
	cpx #.count
	bne -
}

!macro PrintText .addr, .data, .data_end {
  ldx #$00
-
  lda .data, x
  bpl +
  eor #$80
  bne ++
+
  and #$3f
++
  sta .addr, x
  inx
  cpx #.data_end - .data
bne -
}

!macro PrintText_1x2 .addr, .data, .data_end {
  ldx #$00
-
  lda .data, x
  bpl +
  eor #$80
  bne ++
+
  and #$3f
++
  sta .addr, x
  ora #$40
  sta .addr + 40, x
  inx
  cpx #.data_end - .data
bne -
}

!macro ColorWashLeft .addr, .data, .data_end {
  lda .data
  sta .data_end
  ldx #$00
-
  lda .data + $01, x
  sta .data, x
  ;lda .data, x
  sta .addr, x
  inx
  cpx #.data_end - .data
  bne -
}

!macro ColorWashRight .addr, .data, .data_end {
  lda .data_end
  sta .data
  ldx #.data_end - .data
-
  lda .data - $01, x
  sta .data, x
  ;lda .data, x
  sta .addr - 1, x
  dex
  bne -
}

!macro LowerCase {
  lda #$17
  sta $d018
}

!macro UpperCase {
  lda #$15
  sta $d018
}

!macro CheckSpaceKey {
.loop
  lda $dc01
  cmp #$ef
  bne .loop
}

!macro Wait .delay {
	ldy #.delay
-
  dey
	bne -
}

!macro Wait .delay_hi, .delay_lo {
  ldx #.delay_hi
--
	ldy #.delay_lo
-
  dey
	bne -
  dex
  bne --
}

!macro SetColors .color {
	lda #.color
  sta $d020
	sta $d021
}

!macro SetColors .border, .back {
	lda #.border
  sta $d020
	lda #.back
	sta $d021
}

!macro RunStopRestore .enable {
  !if (.enable) {
    lda #$ed
    sta $0328
  } else {
    lda #$ea
    sta $0328
  }
}

!macro ShiftCommodore .enable {
  !if (.enable) {
    lda #$00
    sta $0291
  } else {
    lda #$80
    sta $0291
  }
}

!macro SetRaster .raster, .addr {
	lda #<.addr
	sta $0314
	lda #>.addr
	sta $0315
  lda #.raster
	sta $d012
  lda $d011
	!if (.raster > 255) {
    ora #$80
  } else {
	  and #$7f
  }
	sta $d011
}

!macro EnableRasters .raster, .addr {
  sei
  +SetRaster .raster, .addr
  lda #$7f
	sta $dc0d
  lda $d01a
	ora #$01
	sta $d01a
  lda #$01
	sta $d019
	cli
}

!macro DisableRasters {
  sei
  ; Work In Progress
 	cli
}