
chrout = $ffd2

atnom = %00001000
clkom = %00010000
dtaom = %00100000
clkim = %01000000
dtaim = %10000000
defm  = %00000111

size  = $2f
start = $2b
count = $2d
in    = $fe
ystr  = $ff

*=$cf00

st    lda #defm
      sta $dd00
main
      ldx #waitm & $ff
      ldy #waitm >> 8
      jsr txtout

      sei
      jsr bytein
      sta size
      jsr bytein
      sta size+1
      jsr bytein
      sta run
      jsr bytein
      sta run+1
      jsr bytein
      sta start
      sta count
      clc
      adc size
      sta e1
      php
      jsr bytein
      sta start+1
      sta count+1
      plp
      adc size+1
      sta e2

      ldy count
      lda #0
      sta count
m2
      jsr bytein
      ldx #$34
      stx $01
      sta (count),y
      ldx #$37
      stx $01
      iny
      bne m3
      inc count+1
m3
e1 =*+1
      cpy #0
      bne m2
      lda #'#'
      jsr chrout
      sei
      lda count+1
e2 =*+1
      cmp #0
      bne m2

m6
      sty count
      cli
      ldx #bloadedm & $ff
      ldy #bloadedm >> 8
      jsr txtout

      lda run
      cmp #$fe
      bne m4
      ldx run+1
      cpx #$ff
      bne m4
      rts

m4    and run+1
      cmp #$ff
      bne m5
      jmp main
m5
run =*+1
      jsr $0000
      jmp st


bytein
      sty ystr
      ldy #3
b3    ldx #defm | atnom
      lda #clkim
b1    bit $dd00
      bne b1
      lda $dd00
      stx $dd00
      asl A
      rol in
      ldx #defm
      lda #clkim
b2    bit $dd00
      beq b2
      lda $dd00
      stx $dd00
      asl A
      rol in
      dey
      bpl b3
      ldy ystr
      lda in
      eor #$ff
      rts

txtout
      stx $fe
      sty $ff
      ldy #$00
t1    lda ($fe),y
      beq t2
      jsr chrout
      iny
      bne t1
t2    rts


waitm
      .text "WAITING FOR TRANSFER..."
      .byte 13,0
bloadedm
      .byte 13
      .text "BLOCK LOADED."
      .byte 13,0

.end

