# define INT_VAC 152
# define VEC_ADDR_ADDR INT_VAC * 256 + 255
# define INT_ADDR VEC_ADDR_ADDR + 2

# define bank0 24 
# define bank1 25
# define bank2 26
# define bank3 27
# define bank4 28
# define bank6 30
# define bank7 31


#define default_bank bank0


                ;; banks 0, 1, 3, 4, 6
                ;; Bit 3: Select normal (0) or shadow (1) screen to be displayed. The normal screen is in bank 5, whilst the shadow screen is in bank 7.
                ;; Bit 4: ROM select. ROM 0 is the 128k editor and menu system; ROM 1 contains 48K BASIC.
                ;; Bit 5: If set, memory paging will be disabled and further output to this port will be ignored until the computer is reset.
                ;; 24+



	MODULE	?BANK_CALL_DIRECT_EXAF_L08


        RSEG    UDATA0
        RSEG    IDATA0
        RSEG    CDATA0


	RSEG	RCODE



	PUBLIC	?BANK_CALL_DIRECT_EXAF_L08
	PUBLIC  ?BANK_FAST_LEAVE_L08
	PUBLIC 	?CLZ80B_4_06_L00
	PUBLIC  seg_init
	PUBLIC  ?S_MULASG_L02
	PUBLIC  ?C_MULASG_L01	
	PUBLIC  ?S_MUL_L02
	PUBLIC  ?US_RSH_L02
	PUBLIC  get_r
	PUBLIC  push_all
	PUBLIC  pop_all
	PUBLIC  Pr_Sym
	PUBLIC  input
	PUBLIC  ?C_MUL_L01
	PUBLIC  ?US_RSHASG_L02
	PUBLIC	?SL_CMP_L03
	PUBLIC	?SL_MOD_L03
	PUBLIC	?L_INC_L03
	PUBLIC	?SL_DIV_L03
	PUBLIC	?L_DEC_L03
	PUBLIC	?L_SUBASG_L03
	PUBLIC	?L_MUL_L03
	PUBLIC	?SS_DIV_L02
	PUBLIC	?L_ADDASG_L03
	PUBLIC	?US_MOD_L02
	PUBLIC	?US_DIVASG_L02
	PUBLIC	?US_DIV_L02
	PUBLIC	?SS_MOD_L02
	
	PUBLIC  ?L_MULASG_L03
	PUBLIC  ?UC_DIV_L01
	PUBLIC	?C_V_SWITCH_L06
	PUBLIC	?SS_RSH_L02
	PUBLIC	?S_LSHASG_L02
	PUBLIC	?SC_DIV_L01
	PUBLIC  ?S_S_SWITCH_L06
	PUBLIC 	vg_select_drive
	PUBLIC  vg_read_sector1
	PUBLIC  vg_read_sector2
	PUBLIC  ?C_S_SWITCH_L06
	PUBLIC  output
	PUBLIC  memset

	PUBLIC  decode_lzss_step
	PUBLIC  ?S_LSH_L02
	PUBLIC  space_buf1
    PUBLIC  space_buf2
	PUBLIC  space_buf0  
	PUBLIC  space_buf3
	PUBLIC  space_buf4



	PUBLIC	memcpy
	PUBLIC	strcpy
	PUBLIC	strlen
	PUBLIC	strncmp
	PUBLIC	strncpy
	PUBLIC	strcmp
	PUBLIC	memcmp
  
    PUBLIC vg_init_drive
	PUBLIC strcat
	PUBLIC printstring
    PUBLIC bzero1
	PUBLIC bzero
	PUBLIC attr_too_old

	PUBLIC ?C_LSH_L01
	
	public  ?UC_MOD_L01
	public  memmove

	PUBLIC  di_
    PUBLIC  ei_


di_:
        di
        ret

ei_:
        ei
        ret


memmove:
    push    ix
    ld      ix,0
    add     ix,sp

    push    bc
    push    de
    ld      l,c
    ld      h,b
    ld      c,(ix+4)
    ld      b,(ix+5)
    ldir
    pop     de
    pop     bc

    pop     ix
    ret

?UC_MOD_L01:    //----------------------//  
    push    bc  
    push    de  
    ld    c,a  
    call    ?C_DIVMOD_L01  
    ld    a,d  
    pop    de  
    pop    bc  
    ret  

?C_LSH_L01:     //----------------------//  
    inc     b  
    dec     b  
    ret     z  
C_LSH_L01_0  
    add     a,a  
    djnz    C_LSH_L01_0  
    ret  


enter_p7:
			LD   A, bank7
			LD   BC, 32765
			OUT  (c), a	
			
			ret

leave_p7:
			LD   A, (CUR_BANK_NUM)
			LD   BC, 32765
			OUT  (c), a			
			ret

attr_too_old:

  
  
  call enter_p7
	
  LD hl, 55552
  LD BC, 511
  LD A,  71
  CALL FILL
 
  call leave_p7
  
  
  
  ret 

bzero1:

  
  
  call enter_p7		

  LD hl, 51200
  LD BC, 4095
  LD A,  0
  CALL FILL

  call leave_p7		
			

  

  ret

bzero:
   
   push DE
   pop HL
   DEC BC
   LD A, 0
   CALL FILL

   ret 


printstring:
  
   DEC E
   push BC
   PUSH DE

   LD B, 32

LPS:

   pop DE
   pop HL

   LD A, (HL)
   CP 0
   RET Z
   
   INC HL
   INC E
   push HL
   PUSH DE
 
   PUSH BC

   call Pr_Sym1

   POP BC

   DJNZ LPS		
   
   pop hl
   pop hl

	
   RET


strcat:     //----------------------//       
    call     ?ENT_PARM_DIRECT_L09  
    ld       e,c  
    ld       d,b  
    ld       l,(ix+2)  
    ld       h,(ix+3)  
    call     ?STRCAT_L11  
    jp       ?LEAVE_DIRECT_L09


?STRCAT_L11:     //----------------------//  
    push     hl  
    push     de  
    push     bc  
    push     af  
    xor      a  
    ld       b,a  
    ld       c,a  
    cpir  
    dec      hl  
    ex       de,hl  
STRCAT_L11_0  
    cp       (hl)  
    ldi  
    jr       nz,STRCAT_L11_0  
    pop      af  
    pop      bc  
    pop      de  
    pop      hl  
    ret  

;device fd
;external low-level disk driver v1.3
;for rawr disk distribution system
;by michael borisov (c) 1996

; 0 - init
; 1 - set drive (>a - number drive 0,1,2,3)
; 2 - read (>hl - to address, >de - start track/block, >b - blocks to read)
; 3 - write (>hl - from address, >de - start track/block, >b - blocks to write)



fd_clr_error
	ld a, 255
	ld (fd_err_co),a
	ret

vg_init_drive

	push bc

	ld a, E
	ld (fd_drive_no), A
	call fd_motor_on
	call fd_res_head

	pop bc

	ret



fd_res_head
	di
	ld a,8
	jr fd_posrt

fd_motor_on
	di
	;;call fd_inifdc
	call fd_get_pos
	call fd_position
	ld hl,8500
	jp fd_pause

fd_position
	di
	srl a
	call fd_store_pos
	ld c,127
	call fd_out_c_a
	ccf
	sbc a,a
	and %00010000 ;side
	ld b,a
	ld a,(fd_drive_no)
	or b
	or %00001100
	ld c,255
	call fd_out_c_a
;	call fd_break_key
	ld c,31
	ld a,24 ;position cmd
fd_posrt
	call fd_out_c_a
	call fd_wt_irq
	ret

fd_01 ;set drive:
	ld a,c
	and 3
	ld (fd_drive_no),a
	call fd_get_pos
	ld c,63
	call fd_out_c_a
	call fd_clr_error
	ret

fd_02 ;readgr:	
	ld a,128
	ld c,0

	ld (fd_rwcmd+1),a
	ld a,c
	ld (fd_dmatyp+1),a
	ld a,b
	ld (fd_sec_remain),a
	ld (fd_err_sp),sp
	call fd_clr_error
fd_rwgpos
	ld a,d
	call fd_position

fd_rwgnxt
	ld (fd_dma_addr),hl
	ld (fd_rw_loc),de

fd_rwretr
	ld de,(fd_rw_loc)
	ld hl,(fd_dma_addr)
	;call	break_key
	ld c,95
	ld a,e
	inc a
	call fd_out_c_a
fd_rwcmd
	ld a,0
	ld c,31
	call fd_out_c_a
	ld c,127
fd_dmatyp
	ld a,0
	or a
	jr nz,fd_rwgr0
	call fd_dma_rd
	jr fd_rwgr1
fd_rwgr0
	call fd_dma_wr
fd_rwgr1
	ld a,b
	or a
	ld a,1	;no disk
	jp z,fd_err_ret
	call fd_in_1f
	ld c,a
	and 31
	jr z,fd_rwgr2
	ld a,4	;snf/crc err
	jp fd_err_ret

fd_rwgr2
	bit 6,c
	ld a,2	;wprt
	jp nz,fd_err_ret
fd_rwignr	
	ld de,(fd_rw_loc)
	inc e
	bit 4,e	;16 secs/trk
	jr z,fd_xalal
	ld e,0
	inc d
	scf
fd_xalal
	ld a,(fd_sec_remain)
	dec a
	ld (fd_sec_remain),a
	ret z
	jr nc,fd_rwgnxt
	jr fd_rwgpos

fd_err_ret
	ld	(fd_err_co),a
	ld	a,208
	ld	c,31
	call	fd_out_c_a
	ld	sp,(fd_err_sp)
	ret

fd_store_pos
	push	hl
	push	af
	ld	hl,fd_drv_head
	ld	a,(fd_drive_no)
	add	a,l
	ld	l,a
	adc	a,h
	sub	l
	ld	h,a
	pop	af
	ld	(hl),a
	pop	hl
	ret

fd_get_pos
	push	hl
	ld	hl,fd_drv_head
	ld	a,(fd_drive_no)
	add	a,l
	ld	l,a
	adc	a,h
	sub	l
	ld	h,a
	ld	a,(hl)
	pop	hl
	ret

fd_pause
	push	af
fd_pau_0
	dec	hl
	ld	a,h
	or	l
	jr	nz,fd_pau_0
	pop	af
	ret

;'radisk v1.3 (c) 1996, michael borisov'
;tr-dos port routines

fd_out_c_a
	push	hl
	ld	hl,10835
	ex	(sp),hl
	jp	15664

fd_dma_rd
	push	hl
	ld	hl,16341
	ex	(sp),hl
	jp	15664

fd_dma_wr
	push	hl
	ld	hl,16314
	ex	(sp),hl
	jp	15664

fd_wt_irq
	push	hl
	ld	hl,16357
	ex	(sp),hl
	jp	15664

fd_irz
	ld	hl,16179
	push	hl
	jp	15664

fd_in_1f
	push	hl
	push	bc
	push	de
	xor	a
	ld	c,63
	call	fd_out_c_a
	ld	a,10
	ld	c,95
	call	fd_out_c_a
	ld	d,1
	call	fd_irz
	call	fd_get_pos
	ld	c,63
	call	fd_out_c_a
	ld	a,b
	pop	de
	pop	bc
	pop	hl
	ret


fd_drive_no
		defb	02

fd_drv_head
		defb	00	;a
		defb	00	;b
		defb	00	;c
		defb	00	;d

fd_ttbl_adr
		defw	0000
fd_dma_addr
		defw	0000
fd_rw_loc	defw	0000
fd_sec_remain
		defb	00
fd_err_sp	defw	0000
fd_err_co	defb	255


?ENT_AUTO_DIRECT_L09:     //----------------------//   
    pop      hl  
    push     bc  
    push     de  
    push     ix  
    ld       ix,0  
    add      ix,sp  
    ld       e,(hl)  
    inc      hl  
    ld       d,(hl)  
    inc      hl  
    ex       de,hl  
    add      hl,sp  
    ld       sp,hl  
    ex       de,hl  
    jp       (hl)  
      

strncpy:     //----------------------//  
    call     ?ENT_AUTO_DIRECT_L09   
    defw     0fffeh  
    push     iy  
    ld       e,(ix+8)  
    ld       d,(ix+9)  
    ld       l,(ix+2)  
    ld       h,(ix+3)  
    push     hl  
    pop      iy  
    ld       (ix-2),l   
    ld       (ix-1),h   
strncpy_0        
    ld       a,e  
    ld       h,d  
    dec      de  
    or       h  
    jr       z,strncpy_2  
    ld       l,(ix+4)  
    ld       h,(ix+5)  
    inc      hl  
    ld       (ix+4),l  
    ld       (ix+5),h  
    dec      hl  
    ld       b,(hl)  
    ld       (iy+0),b  
    inc      b  
    dec      b  
    inc      iy  
    jr       nz,strncpy_0  
strncpy_1  
    ld       a,e  
    ld       h,d  
    dec      de  
    or       h  
    jr       z,strncpy_2  
    ld       (iy+0),0  
    inc      iy  
    jr       strncpy_1  
strncpy_2  
    ld       l,(ix-2)  
    ld       h,(ix-1)  
    pop      iy  
    jp       ?LEAVE_DIRECT_L09   


strncmp:     //----------------------//    
    push    iy  
    push    ix  
    push    bc  
    pop     ix  
    push    de  
    pop     iy  
_strncmp_0:  
    ld        hl,6  
    add        hl,sp  
    ld        a,(hl)  
    inc        hl  
    or        (hl)  
    jr        z,_strncmp_1  
    ld        a,(iy+0)  
    cp        (ix+0)  
    inc        ix  
    jr        nz,_strncmp_1  
    ld        a,(iy+0)  
    or        a  
    inc        iy  
    jr        z,_strncmp_2  
    dec        hl  
    ld        c,(hl)  
    inc        hl  
    ld        b,(hl)  
    dec        bc  
    ld        (hl),b  
    dec        hl  
    ld        (hl),c  
    jr        _strncmp_0  
_strncmp_1:  
    dec        hl  
    ld        a,(hl)  
    inc        hl  
    or        (hl)  
    jr        z,_strncmp_2  
    ld        c,(ix-1)  
    ld        b,0  
    ld        h,b  
    ld        l,(iy+0)  
    sbc        hl,bc  
    jr        _strncmp_3  
_strncmp_2:  
    ld        l,a  
    ld        h,a  
_strncmp_3:  
    pop        ix  
    pop        iy   
    ret  


?STRLEN_L11:     //----------------------//  
    push     bc  
    push     af  
    xor      a  
    ld       b,a  
    ld       c,a  
    cpir  
    ld       hl,0ffffh   
    sbc      hl,bc  
    pop      af  
    pop      bc  
    ret 

strlen:     //----------------------//   
    call     ?ENT_PARM_DIRECT_L09  
    ex       de,hl  
    call     ?STRLEN_L11  
    jp       ?LEAVE_DIRECT_L09 


?STRCPY_L11:  
    push    af  
    push    hl  
    push    de  
    push    bc  
    xor     a  
    ex      de,hl  
STRCPY_L11_l0          
    cp      (hl)  
    ldi  
    jr      nz,STRCPY_L11_l0  
    pop     bc  
    pop     de  
    pop     hl  
    pop     af  
    ret  


strcpy:     //----------------------//  
    call     ?ENT_PARM_DIRECT_L09  
    ld       e,c  
    ld       d,b  
    ld       l,(ix+2)  
    ld       h,(ix+3)  
    call     ?STRCPY_L11  
    jp       ?LEAVE_DIRECT_L09   


?MEMCMP_L11:     //----------------------//  
    push     af  
    push     bc   
    push     de  
    ld       a,c  
    or       b  
    jr       z,MEMCMP_L11_1  
MEMCMP_L11_0      
    ld       a,(de)  
    cpi  
    jr       nz,MEMCMP_L11_2  
    inc      de   
    jp       pe,MEMCMP_L11_0  
MEMCMP_L11_1      
    ld       hl,0  
    jr       MEMCMP_L11_3  
MEMCMP_L11_2      
    push     af  
    pop      hl  
    ld       h,l  
MEMCMP_L11_3      
    pop      de  
    pop      bc   
    pop      af  
    ret  


memcmp:        //----------------------//  
    call    ?ENT_PARM_DIRECT_L09  
    ld        c,(ix+8)  
    ld        b,(ix+9)  
    ld        l,(ix+4)  
    ld        h,(ix+5)   
    call    ?MEMCMP_L11     
    jp        ?LEAVE_DIRECT_L09  
      
/******************************************************************************/  
memcpy:        //----------------------//  
    call    ?ENT_PARM_DIRECT_L09  
    ld        c,(ix+8)  
    ld        a,c  
    ld        b,(ix+9)  
    ld        l,(ix+4)  
    ld        h,(ix+5)   
    or        b  
    jr        z,_memcpy_0  
    push    de  
    ldir  
    pop        de  
_memcpy_0:  
    ex        de,hl    
    jp        ?LEAVE_DIRECT_L09  




#define SOURCE_POINTER 0
#define mask 2
#define buf 3
#define r_r 4
#define DECODER_BUFFER_POINTER 6
#define addr   8
#define OUTPUT 10
#define NUM_B 11 
#define best_J 13
#define best_I 14
#define SSS    15
#define SPACE  16
    /// 17 bytes  total


FILL:

;; HL = start address of block

;; DE = HL + 1
ld e,l
ld d,h
inc de

;; initialise first byte of block
;; with data byte (&00)
ld (hl),A
	
;; BC = length of block in bytes
;; HL+BC-1 = end address of block


;; fill memory
ldir

RET



SB_SPACE  DEFS 2

space_buf0:
        LD (SB_SPACE), DE

ret


space_buf1:

LD   A, bank6

JB1:
push BC
LD   BC, 32765
OUT  (c), a

POP bc

LD H, D
LD L, E
LD A, (SB_SPACE)

call FILL

call leave_p7

ret


space_buf2:
LD   A, bank7
JR JB1

space_buf3:
LD   A, bank4
JR JB1

space_buf4:
LD   A, bank2
JR JB1
			

get_bit: ; B=NBITS IX=POINTER

        LD HL, 0 ;  REZ, z
        LD A, (IX+mask) ; mask
        LD C, (IX+buf) ; buf

LOOP:
        CP 0 ;          ;mask =0 ?
        JR NZ, NEXT
                        ;mask==0 вгв
	

        LD D,(IX+SOURCE_POINTER+1)
        LD E,(IX+SOURCE_POINTER)
	
	LD A, (DE) 
	LD C, A		; g[NN].buf = fgetc(NN);

        INC DE ; NN++

        LD (IX+SOURCE_POINTER+1), D
        LD (IX+SOURCE_POINTER), E ; store NN++

        LD A, 128                 ; g[NN].mask = 128;

NEXT:
        ADD HL, HL ; x <<= 1;
	
	LD D, A
	AND C	   ; if (g[NN].buf & g[NN].mask)
	JR Z, NNN

	INC HL     ; x++

NNN:
	LD A, D
 	RRA	   ;g[NN].mask >>= 1;

	DJNZ LOOP ; B used

     LD(IX+mask), A
     LD(IX+buf), C

     RET



decode:

    LD H,(IX+DECODER_BUFFER_POINTER+1)
    LD L,(IX+DECODER_BUFFER_POINTER); HL=buffer[
    push hl
    pop  iy
	
    LD A, (IX+NUM_B)
    CP 0
    JR NZ, LV

    LD A, (IX+NUM_B+1)
    CP 0
    JR NZ, LV
		


    LD B, 1	
    call get_bit	       	;c = getbit(1, n);

    ld a, l
    cp 0				
    jr z, SL
        			;; if (c)
    LD B, 8
    call get_bit		; 
    LD A, L 			; c = getbit(8, n);



       LD (IX+OUTPUT), A        //  fputc (c);   


       LD D, (IX+r_r+1)                   ; DE=r
       LD E, (IX+r_r)


       push iy
       pop hl	

       ADD HL, DE
       LD (HL), A
       INC DE                              ; buffer[n][g[n].r++] = c;

       LD A, D
       AND (IX+SSS)
       LD D, A  ;g[n].r &= (N - 1);

       LD (IX+r_r+1), D
       LD (IX+r_r), E                       ;; store r

	
       RET	
SL:	

    LD B, (IX+best_I)
    call get_bit 
    
    LD (IX+addr+1), H
    LD (IX+addr),   L
	

    LD B, (IX+best_J)
    call get_bit  ;  getbit(EJ, n);

    inc hl;        ; J+1
    inc hl

    LD (IX+NUM_B+1), H
    LD (IX+NUM_B),   L

   

LV:

    LD B, (IX+r_r+1)                  ; BC = r_r
    LD C, (IX+r_r)

    LD D, (IX+addr+1)                 ; DE = addr
    LD E, (IX+addr)

 
;;;;;;;


LLL:
    LD A, D
    AND (IX+SSS)
    LD D, A    ;(addr & (N - 1))

    push iy
    pop hl     ;HL=buffer[

    ADD HL, DE    
    LD A, (HL)  ;c = buffer[n][addr & (N - 1)];
    
    INC DE     ;addr++



    LD (IX+OUTPUT), A // fputc(a) 

    push iy
    pop hl            ;HL=buffer[

    ADD HL, BC
    LD (HL), A   ; buffer[n][g[n].r++] = c;
    INC BC       ; r++                          

    LD A, B
    AND (IX+SSS)
    LD B, A       ; r_r &= (N - 1);
   



    LD (IX+r_r+1), B                  ; store r_r
    LD (IX+r_r),   C


    LD (IX+addr+1), D                 ; store addr
    LD (IX+addr),   E


    LD H, (IX+NUM_B+1)
    LD L, (IX+NUM_B)
   
    DEC HL

    LD (IX+NUM_B+1), H
    LD (IX+NUM_B),   L		      ; dec NUM_B

    RET




decode_lzss_step:

    push iy	
    push ix



# define ARR_LEN 17

    LD B, 13
    push DE
    pop  IX
    LD DE, ARR_LEN

LP1:
    push BC
    push DE


	LD A, (IX+SPACE)
	
	CP 1
	JR Z, L2
	
	CP 2
	JR Z, L3
	
	CP 3
	JR Z, L4
		
    LD   A, bank6
	JR  LS
L2:
    LD   A, bank7
	JR LS

L3:
    LD   A, bank4
	JR LS
	
L4:
    LD   A, bank2
	
LS:
LD   BC, 32765
OUT  (c), a	


    call decode
  
    pop DE
    pop BC

    ADD IX, DE

    DJNZ LP1


	call leave_p7;



    pop ix
    pop iy

    ret






?MEMSET_L11:          
    push    af  
    push    hl  
    push    de  
    ld      a,b  
    or      c  
    jr      z,MEMSET_L11_l0  
    ld      a,l  
    ld      (de),a  
    dec     bc  
    ld      a,b  
    or      c  
    jr      z,MEMSET_L11_l0  
    ld      l,e  
    ld      h,d  
    inc     de  
    ldir  
MEMSET_L11_l0          
    pop     de  
    pop     hl  
    pop     af  
    ret 

memset:        //----------------------//  
    call    ?ENT_PARM_DIRECT_L09  
    ld        c,(ix+8)  
    ld        b,(ix+9)  
    ld        l,(ix+4)  
    call    ?MEMSET_L11  
    ex        de,hl    
    jp        ?LEAVE_DIRECT_L09  
 

?ENT_PARM_DIRECT_L09:  
    pop     hl  
    push    bc  
    push    de  
    push    ix  
    ld      ix,0  
    add     ix,sp  
    jp      (hl)  
         
?LEAVE_DIRECT_L09:  
    ld      sp,ix  
    pop     ix  
    pop     de  
    pop     bc  
    ret  


output:

 LD   A,C
 LD   B,D
 LD   C,E
 OUT  (c), a

 ret


?C_S_SWITCH_L06          
    ex      (sp),hl  
    push    af  
    push    de  
    ld      d,0  
    ld      a,e  
    sub     (hl)  
    inc     hl  
    ld      e,a  
    sub     (hl)  
    inc     hl  
    ld      a,d  
    sbc     a,(hl)  
    inc     hl  
    jr      nc,C_S_SWITCH_L06_0  
    ex      de,hl  
    inc     hl  
    add     hl,hl  
    add     hl,de  
C_S_SWITCH_L06_0:          
    ld      e,(hl)  
    inc     hl  
    ld      d,(hl)  
    ex      de,hl  
    pop     de  
    pop     af  
    ex      (sp),hl  
    ret    



RS_ADDR DEFS 2
RS_TRK  DEFS 2

vg_read_sector1:
	LD (RS_ADDR), DE
	LD (RS_TRK), BC
	
	ret

vg_read_sector2:

    

	LD B, C		 ; nsec
	
	LD A, (RS_TRK)   
	LD D, A          ;trk ->D

	LD HL, (RS_ADDR) ; addr

        ;; assume E is OK


	call fd_02 ;read


	ld a, (fd_err_co)
	ld h, 0
	ld l, a

	

	RET

vg_select_drive:

	LD C, E
	
	
			
	call fd_01 ;set drive

	

	RET



?S_S_SWITCH_L06:     //----------------------//    
    ex       (sp),hl  
    push     af  
    push     de   
    ld       a,e  
    sub      (hl)  
    inc      hl  
    ld       e,a  
    ld       a,d  
    sbc      a,(hl)  
    inc      hl  
    ld       d,a  
    ld       a,e  
    sub      (hl)  
    inc      hl  
    ld       a,d  
    sbc      a,(hl)  
    inc      hl  
    jp       ?S_SWITCH_END_L06  
      
/******************************************************************************/  
?S_SWITCH_END_L06:     //----------------------//    
    jr       nc,S_SWITCH_END_L06_0  
    ex       de,hl  
    inc      hl  
    add      hl,hl  
    add      hl,de  
S_SWITCH_END_L06_0  
    ld       e,(hl)  
    inc      hl  
    ld       d,(hl)  
    ex       de,hl  
    pop      de   
    pop      af  
    ex       (sp),hl  
    ret  


?C_FIND_SIGN_L01:    //----------------------//  
    bit    7,b  
    jr    z,C_FIND_SIGN_L01_0  
    xor    a  
    sub    b  
    ld    b,a  
    ld    a,1  
C_FIND_SIGN_L01_0  
    bit    7,c  
    ret    z  
    push    af  
    xor    a  
    sub    c  
    ld    c,a  
    pop    af  
    xor    1  
    ret         


?SC_DIV_L01:        //----------------------//  
    push    bc  
    push    de  
    ld    c,a  
    xor    a  
    call    ?C_FIND_SIGN_L01  
    push    af  
    call    ?C_DIVMOD_L01  
    pop    af  
    or    a  
    ld    a,c  
    jr    z,SC_DIV_L01_0  
    inc    a  
    jr    SC_DIV_L01_1  
SC_DIV_L01_0  
   xor    0ffh  
SC_DIV_L01_1      
    pop    de  
    pop    bc  
    ret  


?S_LSHASG_L02:  //----------------------//  
    ld      e,(hl)  
    inc     hl  
    ld      d,(hl)  
    call    ?S_LSH_L02  
    ld      (hl),d  
    dec     hl  
    ld      (hl),e  
    ret  


?S_LSH_L02:     //----------------------//  
    inc     b  
    dec     b  
    ret     z  
    ex      de,hl  
S_LSH_L02_0:          
    add     hl,hl  
    djnz    S_LSH_L02_0  
    ex      de,hl  
    ret 


?SS_RSH_L02:     //----------------------//  
    inc     b  
    dec     b  
    ret     z  
SS_RSH_L02_0  
    sra    d  
    rr    e  
    djnz    SS_RSH_L02_0  
    ret  


?C_V_SWITCH_L06:        //----------------------//  
    ex      (sp),hl  
    push    af  
    push    bc  
    ld      c,(hl)  
    inc     hl  
    ld      b,(hl)  
    inc     hl  
    ld      a,e  
    cpir  
    jr      nz,C_V_SWITCH_L06_0  
    inc     hl  
    inc     hl  
C_V_SWITCH_L06_0  
    add     hl,bc  
    jp      ?V_SWITCH_END_L06  
/******************************************************************************/  
?V_SWITCH_END_L06:         
    add     hl,bc  
    add     hl,bc  
    ld      a,(hl)  
    inc     hl  
    ld      h,(hl)  
    ld      l,a  
    pop     bc  
    pop     af  
    ex      (sp),hl  
    ret 



?UC_DIV_L01:        //----------------------//  
    push    bc  
    push    de  
    ld    c,a  
    call    ?C_DIVMOD_L01  
    ld       a,c               
    xor    0ffh  
    pop    de  
    pop    bc  
    ret  
    

?L_MULASG_L03:          //----------------------//  
    push    bc  
    push    de  
    ld      e,(hl)  
    inc     hl  
    ld      d,(hl)  
    inc     hl  
    ld      c,(hl)  
    inc     hl  
    ld      b,(hl)  
    ex      de,hl  
    call    ?L_MUL_L03  
    jp      ?L_END_MULDIVASG_L03 

?L_END_MULDIVASG_L03:   //----------------------//  
    ex      de,hl  
    ld      (hl),b  
    dec     hl  
    ld      (hl),c  
    dec     hl  
    ld      (hl),d  
    dec     hl  
    ld      (hl),e  
    ret  


?SL_MOD_L03:        //----------------------//  
    ex    de,hl  
    ex    (sp),hl  
    push    af  
    push    ix  
    ld    ix,0  
    add    ix,sp  
    push    bc  
    push    de  
    ld    d,(ix+9)  
    ld    (ix+9),h  
    ld    e,(ix+8)  
    ld    (ix+8),l  
    ld    a,1  
    call    ?L_FIND_SIGN_L03  
    call    ?L_DIVMOD_L03  
    or    a  
    jr    nz,SL_MOD_L03_0  
    call    ?L_NOT_L03  
    call    ?L_INC_L03  
SL_MOD_L03_0      
    ld    sp,ix  
    pop    ix  
    pop    af  
    pop    de  
    inc    sp  
    inc    sp  
    ret  
      

?L_FIND_SIGN_L03:    //----------------------//  
    bit    7,d  
    jr    z,L_FIND_SIGN_L03_0  
    ld    b,d  
    ld    c,e  
    ld    h,(ix+7)  
    ld    l,(ix+6)  
    call    ?L_NOT_L03  
    call    ?L_INC_L03  
    ld    d,b  
    ld    e,c  
    ld    (ix+7),h  
    ld    (ix+6),l  
    ld    a,1  
L_FIND_SIGN_L03_0      
    bit    7,(ix-1)  
    ret    z  
    push    af  
    ld    b,(ix-1)  
    ld    c,(ix-2)          
    ld    h,(ix-3)  
    ld    l,(ix-4)  
    call    ?L_NOT_L03  
    call    ?L_INC_L03  
    ld    (ix-1),b  
    ld    (ix-2),c  
    ld    (ix-3),h  
    ld    (ix-4),l  
    pop    af  
    xor    1  
    ret  



?L_NOT_L03:    //----------------------//  
    push    af  
    ld    a,l  
    cpl  
    ld    l,a  
    ld    a,h  
    cpl  
    ld    h,a  
    ld    a,c  
    cpl  
    ld    c,a  
    ld    a,b  
    cpl  
    ld    b,a  
    pop    af  
    ret  
      

?L_INC_L03:    //----------------------//  
    inc    l  
    ret    nz  
    inc    h  
    ret    nz  
    inc    c  
    ret    nz  
    inc    b  
    ret  


?SL_DIV_L03:        //----------------------//  
    ex    de,hl  
    ex    (sp),hl  
    push    af  
    push    ix  
    ld    ix,0  
    add    ix,sp  
    push    bc  
    push    de  
    ld    d,(ix+9)  
    ld    (ix+9),h  
    ld    e,(ix+8)  
    ld    (ix+8),l  
    xor    a  
    call    ?L_FIND_SIGN_L03  
    call    ?L_DIVMOD_L03  
    pop    hl  
    pop    bc  
    or    a  
    call    z,?L_NOT_L03  
    call    nz,?L_INC_L03  
    pop    ix  
    pop    af  
    pop    de  
    inc    sp  
    inc    sp  
    ret  
   

?L_DEC_L03:     //----------------------//  
    push     af  
    ld       a,l  
    or       h  
    dec      hl  
    jr       nz,L_DEC_L03_0  
    dec      bc  
L_DEC_L03_0  
    pop      af  
    ret     


?L_SUBASG_L03:  //----------------------//          
    push    af  
    ld      a,(hl)  
    sub     e  
    ld      (hl),a  
    ld      e,a  
    inc     hl  
    ld      a,(hl)  
    sbc     a,d  
    ld      (hl),a  
    ld      d,a  
    inc     hl  
    ld      a,(hl)  
    sbc     a,c  
    ld      (hl),a  
    ld      c,a  
    inc     hl  
    ld      a,(hl)  
    sbc     a,b  
    ld      (hl),a  
    ld      b,a  
    dec     hl  
    dec     hl  
    dec     hl  
    pop     af  
    ret  

?L_MUL_L03:     //----------------------//  
    ex      (sp),hl  
    push    de  
    push    ix  
    ld      ix,0  
    add     ix,sp  
    ld      d,(ix+9)  
    ld      (ix+9),h  
    ld      e,(ix+8)  
    ld      (ix+8),l  
    push    de  
    push    bc  
    push    af  
    ld      hl,0  
    ld      de,0  
    ld      b,0x20  
    ld      c,(ix-3)  
L_MUL_L03_0          
    srl     (ix-1)  
    rr      (ix-2)  
    rr      (ix+7)  
    rr      (ix+6)  
    jr      nc,L_MUL_L03_1  
    ld      a,l  
    add     a,(ix+4)  
    ld      l,a  
    ld      a,h  
    adc     a,(ix+5)  
    ld      h,a  
    ld      a,e  
    adc     a,(ix-4)  
    ld      e,a  
    ld      a,d  
    adc     a,c  
    ld      d,a  
L_MUL_L03_1  
    sla     (ix+4)  
    rl      (ix+5)  
    rl      (ix-4)  
    rl      c  
    djnz    L_MUL_L03_0  
    ld      (ix-3),c  
    ld      b,d  
    ld      c,e  
    pop     af  
    pop     de  
    pop     de  
    pop     ix  
    pop     de  
    inc     sp  
    inc     sp  
    inc     sp  
    inc     sp  
    ret  


?C_DIVMOD_L01:    //----------------------//
    ld    de,9
C_DIVMOD_L01_0
    rl    c
    dec    e
    ret    z
    rl    d
    ld    a,d
    sub    b
    jr    c,C_DIVMOD_L01_0
    ld    d,a
    jr    C_DIVMOD_L01_0

?SS_DIV_L02:        //----------------------//  
    push    af  
    push    bc  
    push    hl  
    xor    a  
    call    ?S_FIND_SIGN_L02  
    push    af  
    call    ?S_DIVMOD_L02  
    pop    af  
    or    a  
    jr    z,SS_DIV_L02_0  
    inc    de  
    jr    SS_DIV_L02_1  
SS_DIV_L02_0      
    ld    a,e  
    cpl  
    ld      e,a  
    ld    a,d  
    cpl  
    ld      d,a  
SS_DIV_L02_1      
    pop    hl  
    pop    bc  
    pop    af  
    ret  

?S_FIND_SIGN_L02:        //----------------------//  
    bit    7,b  
    jr    z,S_FIND_SIGN_L02_0  
    xor    a  
    sub    c  
    ld    c,a  
    ld    a,0  
    sbc    a,b  
    ld    b,a  
    ld    a,1  
S_FIND_SIGN_L02_0              
    bit    7,d  
    ret    z  
    push    af  
    xor    a  
    sub    e  
    ld    e,a  
    ld    a,0  
    sbc    a,d  
    ld    d,a  
    pop    af  
    xor    1  
    ret  


?S_DIVMOD_L02:    //----------------------//  
    ld    hl,0  
    ld    a,11h  
S_DIVMOD_L02_0  
    rl    e  
    rl    d  
    dec    a  
    ret    z  
    rl    l  
    rl    h  
    sbc    hl,bc  
    jr    nc,S_DIVMOD_L02_0  
    add    hl,bc  
    jr    S_DIVMOD_L02_0  


?L_DIVMOD_L03:    //----------------------//  
    push    af  
    ld    hl,0  
    ld    bc,0  
    ld    (ix-8),21h  
    jr    L_DIVMOD_L03_0  
L_DIVMOD_L03_2      
    add    hl,de  
    scf  
L_DIVMOD_L03_0      
    rl    (ix-4)  
    rl    (ix-3)  
    rl    (ix-2)  
    rl    (ix-1)  
    dec    (ix-8)  
    jr    z,L_DIVMOD_L03_1  
    rl    c  
    rl    b  
    rl    l  
    rl    h  
    sbc    hl,de  
    jr    c,L_DIVMOD_L03_2  
    jr    nz,L_DIVMOD_L03_3  
    ld    a,b  
    sub    (ix+7)  
    jr    c,L_DIVMOD_L03_2  
    jr    nz,L_DIVMOD_L03_3  
    ld    a,c  
    sub    (ix+6)  
    jr    c,L_DIVMOD_L03_2  
    jr    L_DIVMOD_L03_4  
L_DIVMOD_L03_3      
    ld    a,c  
    sub    (ix+6)  
L_DIVMOD_L03_4      
    ld    c,a  
    ld    a,b  
    sbc    a,(ix+7)  
    ld    b,a  
    jr    nc,L_DIVMOD_L03_0  
    dec    hl  
    and    a  
    jr    L_DIVMOD_L03_0  
L_DIVMOD_L03_1      
    pop    af  
    push    hl  
    ld    h,b  
    ld    l,c  
    pop    bc  
    ret          
  


?L_ADDASG_L03:  //----------------------//  
    push    af  
    ld      a,(hl)  
    add     a,e  
    ld      (hl),a  
    ld      e,a  
    inc     hl  
    ld      a,(hl)  
    adc     a,d  
    ld      (hl),a  
    ld      d,a  
    inc     hl  
    ld      a,(hl)  
    adc     a,c  
    ld      (hl),a  
    ld      c,a  
    inc     hl  
    ld      a,(hl)  
    adc     a,b  
    ld      (hl),a  
    ld      b,a  
    dec     hl  
    dec     hl  
    dec     hl  
    pop     af  
    ret  


?US_MOD_L02:        //----------------------//  
    push    af  
    push    hl  
    call    ?S_DIVMOD_L02  
    ex    de,hl  
    pop    hl  
    pop    af  
    ret


?US_DIVASG_L02:        //----------------------//  
    ld    e,(hl)  
    inc    hl  
    ld    d,(hl)  
    call    ?US_DIV_L02  
    ld    (hl),d  
    dec    hl  
    ld    (hl),e  
    ret 

?US_DIV_L02:        //----------------------//  
    push    af  
    push    hl  
    call    ?S_DIVMOD_L02  
    ld    a,e  
    cpl  
    ld    e,a  
    ld    a,d  
    cpl  
    ld    d,a  
    pop    hl  
    pop    af  
    ret 


?SS_MOD_L02:    //----------------------//  
    push    af  
    push    hl  
    push    bc  
    ld    a,1  
    call    ?S_FIND_SIGN_L02  
    push    af  
    call    ?S_DIVMOD_L02  
    pop    af  
    ex    de,hl  
    or    a  
    jr    nz,SS_MOD_L02_0  
    xor    a  
    sub    e  
    ld    e,a  
    ld    a,0  
    sbc    a,d  
    ld    d,a  
SS_MOD_L02_0  
    pop    bc  
    pop    hl  
    pop    af  
    ret  





?SL_CMP_L03:     //----------------------//  
    ex       de,hl  
    ex       (sp),hl  
    push     de  
    push     bc  
    push     af  
    push     de  
    push     bc  
    ex       de,hl  
    ld       hl,0fh  
    add      hl,sp  
    ld       a,(hl)  
    xor      80h  
    ld       b,a  
    ld       (hl),d  
    dec      hl  
    ld       c,(hl)  
    ld       (hl),e  
    ex       de,hl  
    pop      hl  
    ld       a,h  
    xor      80h  
    ld       h,a  
    sbc      hl,bc  
    pop      hl  
    jr       nz,SL_CMP_L03_0  
    ex       de,hl  
    dec      hl  
    ld       a,(hl)  
    dec      hl  
    ld       l,(hl)  
    ld       h,a  
    ex       de,hl  
    and      a  
    sbc      hl,de  
SL_CMP_L03_0  
    pop      bc  
    ld       a,b  
    pop      bc  
    pop      hl  
    pop      de  
    inc      sp  
    inc      sp  
    ret      


?US_RSHASG_L02  //----------------------//  
    ld      e,(hl)  
    inc     hl  
    ld      d,(hl)  
    call    ?US_RSH_L02  
    ld      (hl),d  
    dec     hl  
    ld      (hl),e  
    ret  


input:
	ld b, d
	ld c, e
	
	IN    A,(C)
	
	LD L, A

	ret



Pr_Sym: 
	   LD  A, C	
Pr_Sym1:

       LD  H, 32
	   SUB H 
	  
		   LD L,A
           LD H,0
           ADD HL,HL    ;умножаем код символа
           ADD HL,HL    ;на восемь...
           ADD HL,HL
           LD BC, 15616 ;...и складываем с
           ADD HL,BC    ;адресом начала фонта
           LD A,D       ;знакомая нам уже
           AND 7        ;процедурка ;)
           RRCA
           RRCA
           RRCA
           OR E
           LD E,A
           LD A,D
           AND 24
           OR 192   ; 64  for lower
           LD D,A
           
   
		   
		   call enter_p7		

		   LD B,8
   
     PRINT: 
		   LD A,(HL)    ;берем байт из фонта
           LD (DE),A    ;и кладем в экран
           INC HL       ;делаем приращение
           INC D
           DJNZ PRINT   ;переходим по циклу

		   call leave_p7		
		   


           RET          ;и возвращаемся...



BC_CPY DEFS 2
DE_CPY DEFS 2

push_all
	LD (BC_CPY), BC
	LD (DE_CPY), DE
	ret
pop_all
	LD DE, (DE_CPY)
	LD BC, (BC_CPY)
	ret

get_r
	LD A, R
	LD L, A
	ret


?US_RSH_L02:    //----------------------//  
    inc     b  
    dec     b  
    ret     z  
US_RSH_L02_0          
    srl     d  
    rr      e  
    djnz    US_RSH_L02_0                  
    ret 


?S_MUL_L02:             
    push    af  
    push    bc  
    push    hl  
    ld      hl,0  
S_MUL_L02_L0:          
    srl     b  
    rr      c  
    jr      c,S_MUL_L02_L1  
    ld      a,b  
    or      c  
    jr      z,S_MUL_L02_L2  
    sla     e  
    rl      d  
    jr      S_MUL_L02_L0  
S_MUL_L02_L1          
    add     hl,de  
    sla     e  
    rl      d  
    jr      S_MUL_L02_L0  
S_MUL_L02_L2:          
    ex      de,hl  
    pop     hl  
    pop     bc  
    pop     af  
    ret  


?S_MULASG_L02:  
    ld      e,(hl)  
    inc     hl  
    ld      d,(hl)  
    call    ?S_MUL_L02  
    ld      (hl),d  
    dec     hl  
    ld      (hl),e  
    ret  


?C_MUL_L01:     //----------------------//  
    push    bc  
    ld      c,a  
    xor     a  
C_MUL_L01_0          
    srl     b  
    jr      c,C_MUL_L01_1  
    jr      z,C_MUL_L01_2  
    sla     c  
    jr      C_MUL_L01_0  
C_MUL_L01_1  
    add     a,c  
    sla     c  
    jr      C_MUL_L01_0  
C_MUL_L01_2          
    pop     bc  
    ret  


?C_MULASG_L01:  
    push    bc  
    ld      b,a  
    ld      a,(hl)  
    call    ?C_MUL_L01  
    ld      (hl),a  
    pop     bc  
    ret  


seg_init

;---------------------------------------;
; Zero out UDATA0                       ;
;---------------------------------------;
        LD      HL,.SFE.(UDATA0)
        LD      DE,.SFB.(UDATA0)
        CALL    zero_mem

;---------------------------------------;
; Copy CDATA0 into IDATA0               ;
;---------------------------------------;
        LD      DE,.SFB.(IDATA0)                ;destination address
        LD      HL,.SFE.(CDATA0)
        LD      BC,.SFB.(CDATA0)
        CALL    copy_mem


    DI

	LD HL, INT_ADDR
	LD (VEC_ADDR_ADDR), HL

	LD A, INT_VAC
	LD I, A
	IM 2
	EI
	
        ret

	

;---------------------------------------;
; Copy memory                           ;
;---------------------------------------;
copy_mem
        XOR     A
        SBC     HL,BC
        PUSH    BC
        LD      C,L
        LD      B,H                             ; BC - that many bytes
        POP     HL                              ; source address
        RET     Z                               ; If block size = 0 return now
        LDIR
        RET

;---------------------------------------;
; Clear memory                          ;
;---------------------------------------;
zero_mem
        XOR     A
again   PUSH    HL
        SBC     HL,DE
        POP     HL
        RET     Z
        LD      (DE),A
        INC     DE
        JR      again


CUR_BANK_NUM DEFB default_bank

?BANK_CALL_DIRECT_EXAF_L08

;	DI

	cp      0
	jr      z,     LJP   ;; если "нулейвой" банк, то нет переключения, сразу JMP


		cp 	4
		jr	z,     	next4

        cp      2
        jr      z,     	next2

        cp      3
        jr      z,     	next3

        cp      5
        jr      z,     	next5

		
	
        LD A, (CUR_BANK_NUM)
        PUSH AF              ;; CUR_BANK---->stack
		ld 	a,  bank0	
		jr 	next

next2:  
        LD A, (CUR_BANK_NUM)
        PUSH AF              ;; CUR_BANK---->stack	
		ld      a,  bank1
        jr      next

next3:  
        LD A, (CUR_BANK_NUM)
        PUSH AF              ;; CUR_BANK---->stack
		ld      a,  bank3
        jr     next

next4:  
        LD A, (CUR_BANK_NUM)
        PUSH AF              ;; CUR_BANK---->stack
		ld      a,  bank4
		jr     next

next5:  
        LD A, (CUR_BANK_NUM)
        PUSH AF              ;; CUR_BANK---->stack
		ld      a,  bank7
		;jr     next


	

next:
	push BC
	LD   BC, 32765
	OUT  (c), a
	POP  BC         ; переключение страницы

	LD (CUR_BANK_NUM), A

LJP:
	JP     (HL)




?BANK_FAST_LEAVE_L08

    pop AF  ;;  Из стека номер банка восставниваем

	LD   BC, 32765        
	OUT  (c), a	;; переключаем страницу        

	LD (CUR_BANK_NUM), A  ;; номер страницы - в текущую

?CLZ80B_4_06_L00	
	RET






?STRCMP_L11:    
    push     af  
    push     de  
STRCMP_L11_0      
    ld       a,(de)  
    cp       (hl)  
    jr       nz,STRCMP_L11_1      
    inc      de  
    inc      hl  
    or       a  
    jr       nz,STRCMP_L11_0  
    ld       hl,0  
    defb     11  


STRCMP_L11_1  
    rr       h          
;(281)    ld       hl,0                   
;(284)    ld       de,1ccbh                        
    pop      de                              
    pop      af                                
    ret  
	
	
strcmp:     
    call     ?ENT_PARM_DIRECT_L09  
    ld       l,c  
    ld       h,b  
    call     ?STRCMP_L11  
    jp       ?LEAVE_DIRECT_L09       
  

ENDMOD

end	