   .include #system
   .include #macros
   .include #misc

   .if .not .def __RUN
      .vfl_bochum _string1
      .vfl_bochum _hor
      .vfl_bochum _ver
      .vfl_bochum _screen
   .else

      *     = $3000

_string1 = $F0
_hor     = $F2
_ver     = $F3
_screen  = $F4



      poke     _hor,0
      sta      _ver
      s_print  "This is a first test",@s1+@special
:wait
      lda      consol
      cmp      #6
      bne      :wait
      lda      #50
:more:
      pha      
      s_print  "And this the second."
      pla
      sbc      #1
      bcs      :more
      rts
   .endif

; --------------------------------------------------------------
;      Just advance one line, doesn't do anything visibly
; --------------------------------------------------------------
s_return:
   lda      #0
   sta      _hor
   inc      _ver
   ldy      _ver
   cpy      #25
   bcc      :finish
   sta      _ver
:finish
   rts

; --------------------------------------------------------------
; Pokes a string directly into screen memory, computes actual
; screen address from SAVMSC (OS). This could be a bit more
; general for 32 and 48 byte modes, but then why not lift this
; code and modify it ??
; This is intended as a coding shortcut. For any efficiency do
; it yourself.
; with hor/ver (user supplied)
; Returns:
;   X : #characters left in line
;   Y : #length of printed string
;   Carry set, Zero set
; ---------------------------------------------------------------
s_print:
   jsr      _calc_screen      ; calculate _screen address from _hor/_ver
   lda      #40               ; bytes to go until we hit the next line
   sec                        ; only works for 40 cols
   sbc      _hor
   tax                        ; counter in X
:onemore
   lda      (_string1),y      ; get character
   beq      :finish           ; 0 ? then done ->
   cmp      #155              ; RETURN ?
   beq      :iscrlf           ; -> yes
   jsr      scrbyte
   sta      (_screen),y
   inc      _hor
:skip1
   iny                        ; next character
   bne      :skip
   inc      _screen+1
   inc      _string1+1
:skip
   dex                        ; --bytes to go this line
   bne      :onemore          ; still positive then up

   jsr      :scroll
   bne      :onemore

:scroll
   inc      _ver
   lda      _ver              ; check if we went too low
   cmp      #25               ; too low ?
   bcc      :njet             ; no ->
   lda      #0
   sta      _ver              ; yes wrap to top
   dmove    SAVMSC,_screen    ; set _screen to top
   tya                        ; don't forget to sub Y
   sbc      _screen           ; from _screen cause we are keeping
   sta      _screen           ; Y for the _string1
   bcs      :njet
   dec      _screen+1
:njet:
   poke     _hor,0
   ldx      #40
   rts

:iscrlf
   dex                        ; screen remains the same for next INY
   txa                        ; in skip1. 
   adc      _screen           ; Calc new _screen address
   sta      _screen
   jsr      :scroll
   inx                        ; set counter to 41
   bne      :skip1

; --------------------------------------------------------------
; Calc_screen, computes actual screen address from SAVMSC (OS)
; and hor/ver (user supplied)
; Returns:
; SCREEN : actual screen address
;      A : LSB of screen address
;      Y : 0
;      X : <unchanged>
; ---------------------------------------------------------------
_calc_screen:
   lda      SAVMSC+1
   sta      _screen+1
   lda      SAVMSC

   clc
   ldy      _ver
   beq      :done

:loop
   adc      #40
   bcc      :next
   inc      _screen+1
   clc
:next
   dey
   bne      :loop

:done
   adc      _hor
   sta      _screen
   bcc      :fine
   inc      _screen+1
:fine
   rts

; --------------------------------------------------------------
;        Converts a byte into it's screen representation
; there are probably better ways...
; --------------------------------------------------------------
scrbyte:
   pha
   and      #$7F
   cmp      #$20
   bcc      :case1
   cmp      #$60
   bcs      :case3
   pla
   sbc      #$1F
   rts
   
:case1
   pla
   adc      #$40
   rts
   
:case3
   pla
   rts

