; SpecAY Machine Code Routines
;
; Version 0.3-dev

                 ORG 32652
;                 ENT

; First Routine:
; Copies zero-terminated string into BASIC variable. 
;
; Slightly modified version of...
; zx_setstr
; by Stefano Bodrato, 29/06/2006
;
 
                 LD   A,255           ; ASCII code of variable name (97=c)
                 AND  95              ; convert to upper-case
                 LD   HL,65535        ; 65535 is the start address of the zero-terminated string

                 ld   (morevar+1),a
                 ld   (pointer+1),hl
 

                 ld   hl,($5c4b)      ; VARS
loop:            ld   a,(hl)
                 cp   128
                 jr   nz,morevar

                 jr   store           ; variable not found

morevar:
                 cp   0
                 jr   nz,nextvar
 
                 call $19b8           ; get next variable start
                 call $19e8           ; reclaim space (delete)
 
store:
                 ld   bc,0
pointer:
                 ld   de,0            ; point to the string
                 push de
lenloop:
                 inc  bc              ; string length counter
                 inc  de
                 ld   a,(de)
                 and  a
                 jr   nz,lenloop

                 push hl
                 push bc
                 inc  bc
                 inc  bc
                 inc  bc
                 call $1655           ; MAKE-ROOM
                 pop  bc
                 pop  hl

                 ld   a,(morevar+1)
                 ld   (hl),a
                 inc  hl
                 ld   (hl),c
                 inc  hl
                 ld   (hl),b
                 inc  hl
                 pop  de
                 
                 ex   de,hl
                 ldir            
 
                 ret

nextvar:
                 call $19b8           ;get next variable start
                 ex   de,hl
                 jr   loop


; Second Routine:
; Executes code stored within a BASIC string variable

; Put ASCII codes for "c$" into address 32766+32767
; This can be omitted if these bytes are set before run time

;                LD A,99
;                LD (32766),A
;                LD A,36
;                LD (32767),A   

                 ; Save current contents of CH ADD
                 LD BC,(23645)
                 PUSH BC 

                 ; Put 32766 into CH ADD
                 LD BC,32766
                 LD (23645),BC
                
                 ; Invoke LOOKVAR
                 CALL 10418
                
                 ; Restore old CH ADD
                 POP BC
                 LD (23645),BC
                
                 ; LDIR the code into screen memory.
                 ; HL = Source address
                 ; DE = Destination address
                 ; BC = Bytes to copy

                 INC HL
;                 LD BC,(HL)     ; HL points to size of variable
                 LD C,(HL)
                 INC HL
                 LD B,(HL)
                 INC HL
                 LD DE,20480

                 LDIR

                 JP 20480

                 ; Code jumped to ends with RET. 
