Subversion Repositories NedoOS

Rev

Rev 686 | Rev 875 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1.         DEVICE ZXSPECTRUM128
  2.         include "../_sdk/sys_h.asm"
  3.  
  4. ;при закрытии cmd term должен закрыться
  5.  
  6. RECODEINPUT=1
  7.  
  8. READPASTABUF_SZ=80
  9. STDINBUF_SZ=256
  10.  
  11. HTMLTOPY=0
  12. HTMLHGT=25
  13.  
  14. COLOR=7
  15. CURSORCOLOR=0x38
  16.  
  17. REPEATNOKEY=2 ;0
  18.  
  19.         macro BDOSSETPGSSCR
  20.         ld a,(user_scr0_low) ;ok ;pgscr0_0 ;attr
  21.         SETPG32KLOW
  22.         ld a,(user_scr0_high) ;ok ;pgscr0_1 ;text
  23.         SETPG32KHIGH
  24.         endm
  25.  
  26.         org PROGSTART
  27. begin
  28.         ld sp,0x4000
  29.         OS_HIDEFROMPARENT
  30.         ld e,6 ;textmode
  31.         OS_SETGFX
  32.         ld de,ansipal
  33.         OS_SETPAL
  34.         OS_GETMAINPAGES ;out: d,e,h,l=pages in 0000,4000,8000,c000, c=flags, b=id
  35.         ld a,e
  36.         ld (pgscrbuf),a
  37.         push hl
  38.         ld e,l
  39.         OS_DELPAGE
  40.         pop hl
  41.         ld e,h
  42.         OS_DELPAGE
  43.  
  44.         OS_SETSYSDRV
  45.  
  46.         ld de,tpipename
  47.         push de
  48.         OS_OPENHANDLE
  49.         ld a,b
  50.         ld (stdinhandle),a
  51.         pop de
  52.         OS_OPENHANDLE
  53.         ld a,b
  54.         ld (stdouthandle),a
  55.        
  56.         ld e,COLOR
  57.         call BDOS_cls
  58.  
  59.         ld a,(stdinhandle)
  60.         ld e,a
  61.         ld a,(stdouthandle)
  62.         ld d,a
  63.         ld h,0xff ;rnd
  64. ;b=id, e=stdin, d=stdout, h=stderr        
  65.         OS_SETSTDINOUT
  66.  
  67. ;TODO запускать файл, указанный в параметре (по умолчанию cmd, искать в bin)
  68.         ld de,cmd_filename
  69.         OS_OPENHANDLE
  70.         or a
  71.         jr nz,execcmd_error
  72.        
  73.         call readapp ;делает CLOSE
  74.        
  75.         push af
  76.         ld b,a
  77.         ld a,(stdinhandle)
  78.         ld d,a
  79.         ld a,(stdouthandle)
  80.         ld e,a
  81.         ld h,0xff ;rnd
  82. ;b=id, e=stdin, d=stdout, h=stderr        
  83.         OS_SETSTDINOUT
  84.        
  85.         pop af ;id
  86.  
  87.         ld e,a ;id
  88.         ld (waitpid_id),a
  89.         OS_RUNAPP
  90.  
  91.         ld hl,trecode
  92.         ld d,trecodeback/256
  93. maketrecodeback0
  94.         ld e,(hl)
  95.         ld a,l
  96.         ld (de),a
  97.         inc l
  98.         jr nz,maketrecodeback0
  99.  
  100. execcmd_error
  101. mainloop_afternokey
  102. ;если два раза подряд нет событий, то надо делать YIELD, иначе YIELDKEEP
  103. ;т.е. когда нет событий, идёт всё время YIELD. А если событие,пусто,событие,пусто, то всё время YIELDKEEP
  104. ;тогда курсор исчезает - плохо
  105.         if REPEATNOKEY != 0
  106. wasnokey=$+1
  107.         ld a,1
  108.         dec a
  109.         jr nz,mainloop_yieldkeep
  110.         ;scf
  111.         ;ld a,55 ;"scf"
  112.         ;ld (wasnokey),a
  113.         ;jr nc,mainloop_yieldkeep
  114.         endif
  115.        call printcursor
  116.          ld hl,(pr_buf_curaddr)
  117.          ld (wascursorcuraddr),hl        
  118.         call getmousexy
  119.         ld (mousecursor_wasxy),de
  120.         call BDOS_countattraddr_mousecursor
  121.         ld a,(hl)
  122.         cpl
  123.         ld (hl),a
  124.          ;ld (hl),CURSORCOLOR
  125.  
  126.         YIELD
  127.         ld a,(pgscrbuf) ;ok
  128.         SETPG16K
  129.         call BDOS_countattraddr
  130. wascursorcuraddr=$+1
  131.         ld de,killbuf_byte
  132.         ld a,e
  133.         add a,0x40 ;attr
  134.         adc a,0
  135.         ld e,a
  136.         ld a,(de) ;из pgscrbuf
  137.         ld (hl),a;COLOR
  138.  
  139.         jr mainloop_afterkeyq
  140. mainloop_afterkey
  141.         if REPEATNOKEY != 0
  142.         ;ld a,55+128 ;"or a"
  143.         ;ld (wasnokey),a
  144.         ld a,REPEATNOKEY;2
  145. mainloop_yieldkeep
  146.         ld (wasnokey),a
  147.         endif
  148.         YIELDKEEP
  149. mainloop_afterkeyq
  150. mousecursor_wasxy=$+1
  151.         ld de,0
  152.          call getscrbuftop_a
  153.          add a,d ;0..24
  154.          ld h,a
  155.          ld a,e
  156.          or 0x80
  157.          rrca ;(x/2)+0x40 или 0xc0
  158.         add a,0x40 ;attr
  159.         adc a,0
  160.         ld l,a
  161.         ld a,(hl) ;из pgscrbuf
  162.         push af
  163.         call BDOS_countattraddr_mousecursor
  164.         pop af
  165.         ld (hl),a ;screen
  166.  
  167. mainloop_afterredraw
  168.        
  169. waitpid_id=$+1
  170.         ld e,0
  171.         OS_WAITPID ;TODO проверять, что пайп с той стороны не закрыт
  172.         or a
  173.         jp z,quit
  174.        
  175.         jr mainloop_type0_go
  176. mainloop_type0
  177.         YIELDKEEP
  178.  
  179. mainloop_type0_go
  180.         call type_stdin ;stdin to screen
  181.         jr nc,mainloop_type0 ;data present
  182.        
  183.         if 1==0
  184. ;if long time no message from stdin, print cursor
  185.         OS_GETTIMER ;hlde=timer
  186.         push de
  187.         ex de,hl
  188. lastsdtinmsgtimer=$+1
  189.         ld de,0
  190.          ;ld (lastsdtinmsgtimer),hl
  191.         or a
  192.         sbc hl,de ;hl=timer-oldtimer
  193. cursortimelimit=$+1
  194.         ld bc,1
  195.         or a
  196.         sbc hl,bc
  197.         pop hl
  198.         jr c,noprintcursor
  199.          ;ld (lastsdtinmsgtimer),hl
  200.        
  201.         ld a,4
  202.         out (0xfe),a
  203.         call printcursor
  204.         ld a,1
  205.         ld (cursortimelimit),a
  206.  
  207.         ld a,2
  208.         out (0xfe),a
  209.         jr noprintcursorq
  210. noprintcursor
  211.         ld a,7
  212.         out (0xfe),a
  213. noprintcursorq
  214.         endif
  215.  
  216.         GET_KEY ;out: a=key (NOKEY=no key), de=mouse position (y,x), l=mouse buttons (bits 0,1,2: 0=pressed)+mouse wheel (bits 7..4), h=high bits of key|register, bc=keynolang, nz=no focus (mouse position=0, ignore it!)
  217.         jp nz,mainloop_afternokey ;no focus
  218.  
  219.         ;or a ;cp NOKEY ;keylang==0?
  220.         ;jr nz,$+3
  221.         ;cp c ;keynolang==0?
  222.         ;jp z,mainloop_afternokey
  223.         cp key_redraw
  224.         jp z,term_redraw
  225.         ld b,a
  226.  
  227.         ld a,l
  228.         and 0xf0
  229. control_imer_oldmousewheel=$+2
  230.         ld hx,0
  231.         ld (control_imer_oldmousewheel),a
  232.         sub hx
  233.         ;ld (mouse_scrollvalue),a
  234.         jr z,nowheelmove
  235.         jp m,term_pgdown
  236.         jp term_pgup
  237. nowheelmove
  238.         ;ld a,0
  239.         ;ld (mouse_scrollvalue),a ;default scrollvalue
  240.  
  241.         ld a,b
  242.         or a
  243.         jp nz,term_sendchar        
  244. ;no action? mouse coords change is also an action
  245. control_imer_oldmousecoords=$+1
  246.         ld bc,0
  247.         ld (control_imer_oldmousecoords),de
  248.         ld a,b
  249.         sub d
  250.         ld d,a
  251.         ld a,e
  252.         sub c
  253.         ld e,a
  254.         or d
  255.         jr z,nomousemove
  256.         push hl
  257.         call mousemove
  258.         pop hl
  259.         jr sendmouseevent
  260. nomousemove
  261.         ld a,l ;mouse buttons
  262. oldmousebuttons=$+1
  263.         ld h,0
  264.         ld (oldmousebuttons),a
  265.         xor h
  266.         and 7
  267.         jr nz,sendmouseevent
  268.         jp mainloop_afternokey
  269. sendmouseevent
  270.         ld a,l
  271.         cpl
  272.         and 7
  273.         jr z,sendmouseevent_noclick
  274.         call getmousexy
  275.         ld a,d
  276.         or e
  277.         jr nz,sendmouseevent_noclicktopleft
  278.         ld de,tpastaname
  279.         OS_CREATEHANDLE
  280.         ld a,b
  281.         ld (pastahandle),a
  282.        
  283.         ld a,(pgscrbuf)
  284.         SETPG16K
  285.         ld hl,0x4040
  286. savepasta0
  287.         push hl
  288.         call savepastaline
  289.         pop hl
  290.         inc h
  291.         jp p,savepasta0        
  292.         ld a,(pastahandle)
  293.         ld b,a
  294.         OS_CLOSEHANDLE
  295.         jp mainloop_afterkey
  296. sendmouseevent_noclicktopleft
  297.         ld a,d
  298.         sub 24
  299.         or e
  300.         jr nz,sendmouseevent_click
  301.         ld de,tpastaname
  302.         OS_OPENHANDLE
  303.         ld a,b
  304.         ld (pastahandle),a
  305.         ld de,readpastabuf
  306.         push de
  307.         ld hl,READPASTABUF_SZ
  308.         OS_READHANDLE ;hl=реально прочитано
  309.         pop de
  310.         call sendchars
  311.         ld a,(pastahandle)
  312.         ld b,a
  313.         OS_CLOSEHANDLE
  314.         jp mainloop_afterkey
  315. sendmouseevent_noclick
  316.         ld b,'@' ;mouse move
  317.         jr sendmouseevent_ok
  318. sendmouseevent_click
  319.         ld a,l ;mouse buttons
  320.         ld b,1+32
  321.         rra
  322.         jr nc,sendmouseevent_ok
  323.         inc b
  324.         rra
  325.         jr nc,sendmouseevent_ok
  326.         inc b
  327.         rra
  328.         jr nc,sendmouseevent_ok
  329.         ld b,0+32 ;unclick
  330. sendmouseevent_ok
  331. ;send mousemove event
  332.         ld hl,stdoutbuf
  333.         ld (hl),0x1b
  334.         inc hl
  335.         ld (hl),'['
  336.         inc hl
  337.         ld (hl),'M'
  338.         inc hl
  339.         ld (hl),b
  340.         inc hl
  341.         call getmousexy
  342.          ld a,e
  343.          add a,32
  344.         ld (hl),a
  345.         inc hl
  346.          ld a,e
  347.          add a,32
  348.         ld (hl),a
  349.         ld de,stdoutbuf
  350.         ld hl,6
  351.         call sendchars
  352.         jp mainloop_afterkey
  353.        
  354. printcursor
  355.          ;ld hl,4
  356.          ;ld (cursortimelimit),hl
  357.         call BDOS_countattraddr
  358.         ld (hl),CURSORCOLOR
  359.         ret
  360.  
  361. term_sendchar
  362.         cp key_esc
  363.         jr z,term_esckey
  364.         if RECODEINPUT
  365.         call sendkey
  366.         else
  367.         call sendchar_byte_a
  368.         endif
  369.         jp mainloop_afterkey
  370. term_esckey
  371.         if RECODEINPUT
  372.         call sendchar_byte_a
  373.         ld a,key_esc
  374.         call sendchar_byte_a
  375.         else
  376.         call sendchar_byte_a
  377.         endif
  378.         jp mainloop_afterkey
  379.  
  380. term_pgdown
  381.         ld hl,redraw_scroll
  382.         ld a,(hl)
  383.         cp 0;24
  384.         jr z,$+3
  385.         dec a
  386.         ld (hl),a
  387.         call redraw
  388.         jp mainloop_afterredraw
  389. term_pgup
  390.         ld hl,redraw_scroll
  391.         ld a,(hl)
  392.         cp 63 -24
  393.         jr z,$+3
  394.         inc a
  395.         ld (hl),a
  396.         call redraw
  397.         jp mainloop_afterredraw
  398.  
  399. term_redraw
  400.         ld hl,redraw_scroll
  401.         ld (hl),0
  402.         call redraw
  403.         jp mainloop_afterredraw
  404.  
  405. redraw_to_base
  406.         ld hl,redraw_scroll
  407.         inc (hl)
  408.         dec (hl)
  409.         ret z
  410.         ld (hl),0
  411.         push af
  412.         call redraw
  413.         pop af
  414.         ret
  415. redraw
  416. ;scrbuf состоит из строк длиной 256 байт
  417. ;каждая из них из 4 слоёв:
  418. ;+0x40: аналог +0x4000 (text0) ;1
  419. ;+0x80: аналог +0x2000 (attr0) ;3
  420. ;+0xc0: аналог +0x6000 (text1) ;2
  421. ;+0x01: аналог +0x0001 (attr1) ;4
  422.         ld a,(pgscrbuf)
  423.         SETPG16K
  424.         BDOSSETPGSSCR
  425.          call getscrbuftop_a
  426.         ld h,a
  427.         ld de,0xc1c0
  428.         ld b,25
  429. redrawlines0
  430.         push bc
  431.         ld l,0x40
  432.         push de
  433.         call copylinelayer
  434.         set 5,d
  435.         set 7,l
  436.         call copylinelayer
  437.         res 6,d
  438.         res 6,l
  439.         call copylinelayer
  440.         res 5,d
  441.         res 7,l
  442.         call copylinelayer
  443.         pop de
  444.         ex de,hl
  445.         ld bc,0x0040
  446.         add hl,bc
  447.         ex de,hl
  448.         inc h ;TODO nextpg
  449.         pop bc
  450.         djnz redrawlines0
  451.         ret
  452.        
  453. quit
  454. ;cmd closed!!!
  455.  
  456.         dup 2 ;close twice - as stdin and as stdout! на случай, если клиент не закрыл у себя
  457.         ld a,(stdinhandle)
  458.         ld b,a
  459.         OS_CLOSEHANDLE
  460.         ld a,(stdouthandle)
  461.         ld b,a
  462.         OS_CLOSEHANDLE
  463.         edup
  464.         QUIT
  465.  
  466. savepastaline
  467. ;hl=lineaddr
  468. ;текст лежит в 0x40, 0xc0, 0x41, 0xc1,.... 0xfe, 0x7f, 0xff
  469. ;ищем конец строки (первый символ с конца, не равный 0/0x20)
  470.         ld l,0xc0+39 ;end of text
  471.         ld b,80
  472. savepastaline_findend0
  473.         ld a,(hl)
  474.         or a
  475.         jr z,savepastaline_findend0ok
  476.         cp 0x20
  477.         jr nz,savepastaline_findendq
  478. savepastaline_findend0ok
  479.         ld a,l
  480.         sub 0x80
  481.         sbc a,0
  482.         ld l,a
  483.         djnz savepastaline_findend0
  484.         jr savepastaline_skip
  485. savepastaline_findendq
  486. ;hl=end of line
  487. ;сохраняем всю строку до конца
  488.         ld l,0x40 ;start of text
  489. savepastaline0
  490.         ld a,(hl)
  491.         or a
  492.         jr nz,$+4
  493.         ld a,0x20
  494.         push bc
  495.         push hl
  496.         call writechar2pasta
  497.         pop hl
  498.         pop bc
  499.         ld a,l
  500.         add a,0x80
  501.         adc a,0
  502.         ld l,a
  503.         djnz savepastaline0
  504. savepastaline_skip
  505.         ld a,0x0d
  506.         call writechar2pasta
  507.         ld a,0x0a
  508. writechar2pasta
  509.         ld d,trecodeback/256
  510.         ld e,a
  511.         ld a,(de)
  512.         ld de,pastabuf
  513.         ld (de),a
  514.         ld hl,1
  515. pastahandle=$+1
  516.         ld b,0
  517.         OS_WRITEHANDLE
  518.         ret
  519.  
  520. getmousexy
  521. mousexy=$+1
  522.         ld de,0
  523.         ret
  524.  
  525. type_stdin
  526. ;out: CY=no data
  527.         ld de,stdinbuf
  528.         ld hl,STDINBUF_SZ
  529. stdinhandle=$+1
  530.         ld b,0
  531.         ;ld b,0xff
  532.         OS_READHANDLE ;6933t (incl. 5350=ldir 255 bytes)
  533. ;hl=size
  534.         ld a,h
  535.         or l
  536.         scf ;out: CY=no data
  537.         ret z ;jr z,nostdinmsg;mainloop_afterkey
  538.         if REPEATNOKEY != 0
  539.          ld a,REPEATNOKEY;2
  540.          ld (wasnokey),a
  541.         endif
  542.  
  543.         push hl
  544.         call redraw_to_base
  545.          ;OS_GETTIMER ;hlde=timer
  546.          ;ld (lastsdtinmsgtimer),de
  547.         BDOSSETPGSSCR
  548. pgscrbuf=$+1
  549.         ld a,0 ;ok
  550.         SETPG16K
  551.         pop bc
  552.         push bc
  553.         ld b,c
  554.         ld hl,stdinbuf
  555. TERM_ST_SINGLE=1 ;wait for single symbol
  556. TERM_ST_AFTERESC=2 ;after 0x1b
  557. TERM_ST_AFTERESCBRACKET=3 ;after 0x1b [ [number] (might be more digits)
  558. term_prfsmcurstate=$+1
  559.          ld a,TERM_ST_SINGLE
  560.          dec a
  561.          jr nz,term_prfsm
  562. term_print0
  563.         ld a,(hl)
  564.         cp 0x1b+1
  565.         jr c,term_print0_maybecontrolcode
  566.         push hl
  567.         call BDOS_prchar_a_nocrlf ;226/227t
  568.         ;call term_prfsm ;520/521t
  569.         pop hl
  570. term_print0_maybecontrolcodeq
  571.         inc hl
  572.         djnz term_print0
  573.          ld a,TERM_ST_SINGLE
  574.         jp term_print0q
  575. term_print0_maybecontrolcode
  576.         cp 0x1b
  577.         jr z,term_prfsm_afteresc ;next state
  578.         push hl
  579.         call BDOS_prchar_a
  580.         pop hl
  581.         jp term_print0_maybecontrolcodeq
  582.        
  583. term_prfsm
  584.          dec a
  585.          jr nz,term_prfsm_afterescbracket
  586. term_prfsm_afteresc0
  587.         xor a
  588.         ld (term_prfsm_curnumber),a
  589.         ld (term_prfsm_curnumber1),a
  590.         ld (term_prfsm_curnumber2),a
  591.         ld (term_prfsm_curnumber3),a
  592.         ;ld a,(hl) ;todo check bracket
  593.         jp term_prfsm_afterescbracket_ok ;next state
  594. term_prfsm_afteresc
  595.         inc hl
  596.         djnz term_prfsm_afteresc0
  597.          ld a,TERM_ST_AFTERESC
  598.         jp term_print0q
  599.  
  600. term_prfsm_afterescbracket_nosemicolon
  601.         push hl
  602.         call term_prfsm_letter
  603.         pop hl
  604.         jp term_print0_maybecontrolcodeq ;initial state
  605. term_prfsm_afterescbracket
  606. term_prfsm_afterescbracket0
  607.         ld a,(hl)
  608.         sub '0'
  609.         cp 10
  610.         jr nc,term_prfsm_afterescbracket_nonumber
  611.         ;push de
  612.         ld e,a
  613.         ld a,(term_prfsm_curnumber)
  614.         ld d,a
  615.         add a,a
  616.         add a,a
  617.         add a,d
  618.         add a,a ;*10
  619.         add a,e
  620.         ld (term_prfsm_curnumber),a
  621.         ;pop de
  622.         jp term_prfsm_afterescbracket_ok
  623. term_prfsm_afterescbracket_nonumber
  624.         add a,'0'
  625.         cp ';'
  626.         jr nz,term_prfsm_afterescbracket_nosemicolon
  627.         ld a,(term_prfsm_curnumber2)
  628.         ld (term_prfsm_curnumber3),a
  629.         ld a,(term_prfsm_curnumber1)
  630.         ld (term_prfsm_curnumber2),a
  631.         ld a,(term_prfsm_curnumber)
  632.         ld (term_prfsm_curnumber1),a
  633.         xor a
  634.         ld (term_prfsm_curnumber),a
  635. term_prfsm_afterescbracket_ok
  636.         inc hl
  637.         djnz term_prfsm_afterescbracket0
  638.          ld a,TERM_ST_AFTERESCBRACKET
  639. term_print0q
  640.          ld (term_prfsmcurstate),a
  641.         pop hl
  642.         ld bc,STDINBUF_SZ
  643.         or a
  644.         sbc hl,bc
  645.         ret z ;out: NC=data present
  646.         scf
  647.         ret ;no more data
  648.  
  649. sendchar_esckey
  650.         push bc
  651.          ;call forcereprintcursor
  652.         ld hl,stdoutbuf
  653.         ld (hl),0x1b
  654.         inc hl
  655.         ;call sendchar_byte_a
  656.         ld (hl),'['
  657.         ;call sendchar_byte_a
  658.         ld de,stdoutbuf
  659.         ld hl,2
  660.         call sendchars
  661.         pop bc
  662.         jr sendchar_byte
  663.  
  664. sendchar_esckey2
  665.         push bc
  666.         ld hl,stdoutbuf
  667.         ld (hl),0x1b
  668.         inc hl
  669.         ;call sendchar_byte_a
  670.         ld (hl),'['
  671.         inc hl
  672.         ;call sendchar_byte_a
  673.         pop bc
  674.         push bc
  675.         ld (hl),b
  676.         ;call sendchar_byte_a
  677.         ld de,stdoutbuf
  678.         ld hl,3
  679.         call sendchars
  680.         pop bc
  681.         jr sendchar_byte
  682.  
  683.         if 1==0
  684. sendchar_num
  685. ;a=num
  686.         ld c,'0'-1
  687.         inc c
  688.         sub 10
  689.         jr nc,$-3
  690.         push af
  691.         call sendchar_byte
  692.         pop af
  693.         add a,'0'+10
  694.         jr sendchar_byte_a
  695.         endif
  696.  
  697. sendkey
  698. ;key to stdout
  699.         ;cp 0x80
  700.         ;jr nc,sendchar_rustoutf8
  701.         ;cp 0x08 ;backspace
  702.         ;cp 0x0d ;enter
  703.         cp key_left
  704.         ld c,'D'
  705.         jr z,sendchar_esckey
  706.         cp key_right
  707.         ld c,'C'
  708.         jr z,sendchar_esckey
  709.         cp key_down
  710.         ld c,'B'
  711.         jr z,sendchar_esckey
  712.         cp key_up
  713.         ld c,'A'
  714.         jr z,sendchar_esckey
  715.         cp key_del
  716.         ld bc,'3'*256+'~'
  717.         jr z,sendchar_esckey2
  718.         cp key_home
  719.         ld bc,'1'*256+'~'
  720.         jr z,sendchar_esckey2
  721.         cp key_end
  722.         ld bc,'4'*256+'~'
  723.         jr z,sendchar_esckey2
  724.         cp key_ins
  725.         ld bc,'2'*256+'~'
  726.         jr z,sendchar_esckey2
  727.         ld c,a
  728. sendchar_byte
  729.         ld a,c
  730. sendchar_byte_a
  731. sendchar
  732.         ;cp 0x80
  733.         ;jr nc,sendchar_rustoutf8
  734. ;sendchar_byte_a
  735.         ld (stdoutbuf),a
  736.         ld hl,1
  737.         ld de,stdoutbuf
  738. sendchars
  739. ;send chars to stdout (in: de=buf, hl=size, out: A=error)
  740.         ld b,5 ;attempts
  741. sendchars0
  742.         push bc
  743.         push de
  744.         push hl
  745. stdouthandle=$+1
  746.         ld b,0
  747.         OS_WRITEHANDLE ;1436t ;[2718t (1225 before BDOS_writehandle + 195 before BDOS_writehandle_pipe + 477 ..findpipe_byhandle + 301 pipe + 192 end BDOS_writehandle + 326 end BDOS)]
  748.         ld b,h
  749.         ld c,l ;bytes actually written
  750.         pop hl
  751.         pop de
  752.         pop ix
  753.          or a
  754.           ret nz ;error ;TODO обработать? а так пока просто избегаем зацикливания
  755.          sbc hl,bc ;datasize-byteswritten
  756.          ret z
  757.        ;ld a,5 ;m
  758.        ;out (-2),a
  759.          ex de,hl
  760.          add hl,bc ;dataaddr+byteswritten
  761.          ex de,hl
  762. ;hl=remaining data size
  763. ;de=remaining data addr
  764.         push ix
  765.         push de
  766.         push hl
  767.         YIELDKEEP ;2158t
  768.         call type_stdin
  769.         pop hl
  770.         pop de
  771.         pop bc
  772.         djnz sendchars0
  773.         xor a ;z=no error
  774.         ret ;клиент завис, но не сдох
  775.  
  776. term_prfsm_letter
  777.         ;ld hl,term_prfsm_curstate
  778.         ;ld (hl),TERM_ST_SINGLE
  779.          ;ld b,TERM_ST_SINGLE
  780.         cp 'H'
  781.         jr z,term_prfsm_afterescbracket_H
  782.         cp 'G'
  783.         jr z,term_prfsm_afterescbracket_G
  784.         cp 'm'
  785.         jr z,term_prfsm_afterescbracket_m
  786.         cp 'K'
  787.         jr z,term_prfsm_afterescbracket_clearline
  788. ;TODO J etc.
  789.         ;cp 'A' ;cursor_up
  790.         cp 'B'
  791.         jp z,cursor_down
  792.         cp 'C'
  793.         jp z,cursor_right
  794.         cp 'D'
  795.         jp z,cursor_left
  796.         cp 's'
  797.         jp z,cursor_store
  798.         cp 'd' ;NON-STANDARD!
  799.         jp z,term_prfsm_afterescbracket_scrolldown
  800.         cp 'u' ;NON-STANDARD!
  801.         jp z,term_prfsm_afterescbracket_scrollup ;+cursor remember
  802.         ;cp '~'
  803.         ;jr z,term_prfsm_afterescbracket_tilde
  804.         ret
  805. ;term_prfsm_afterescbracket_tilde
  806. ;        ret
  807.  
  808. term_prfsm_afterescbracket_clearline
  809. ;не двигает курсор
  810.         push bc
  811.         ld de,(pr_textmode_curaddr)
  812.         ld hl,(pr_buf_curaddr)
  813.         ld bc,0x0040
  814. term_prfsm_afterescbracket_clearline0
  815.         xor a
  816.         ld (de),a
  817.         ld a,d
  818.         xor 0x60 ;attr + 0x20
  819.         ld d,a
  820.         and 0x20
  821.         jr nz,$+3
  822.         inc e
  823.         xor a
  824.         ld (de),a
  825.         set 6,d
  826.         ld (hl),a
  827.         ld a,l
  828.         add a,c;0x40 ;attr
  829.         adc a,b;0
  830.         ld l,a
  831.         ld (hl),b;0
  832.         add a,c;0x40 ;text (next)
  833.         ld l,a
  834.         cp 0x40+(80/2)
  835.         jp nz,term_prfsm_afterescbracket_clearline0
  836.         pop bc
  837.         ret
  838.  
  839. term_prfsm_afterescbracket_G
  840.         ld a,(term_prfsm_curnumber) ;column
  841.         dec a
  842.         ld e,a
  843.         jp BDOS_setx
  844.  
  845. term_prfsm_afterescbracket_H
  846.         ld a,(term_prfsm_curnumber1) ;row
  847.         dec a
  848.         ld d,a
  849.         ld a,(term_prfsm_curnumber) ;column
  850.         dec a
  851.         ld e,a
  852.         jp BDOS_setxy
  853.        
  854.         if 1==0
  855.         ;jp forcereprintcursor ;не прокатит? в начале печати cmd тоже setxy
  856. forcereprintcursor
  857.         ;push de
  858.         ;push hl
  859.         ;OS_GETTIMER ;hlde=timer
  860.         ;dec d
  861.         ;ld (lastsdtinmsgtimer),de
  862.          ld hl,0;2
  863.          ld (cursortimelimit),hl
  864.         ;pop hl
  865.         ;pop de
  866.         ret
  867.         endif
  868.        
  869. term_prfsm_afterescbracket_m
  870. ;CSI Pm m Character Attributes (SGR)
  871. ;Ps = 30  Set foreground color to Black.
  872. ;Ps = 31  Set foreground color to Red.
  873. ;Ps = 32  Set foreground color to Green.
  874. ;Ps = 33  Set foreground color to Yellow.
  875. ;Ps = 34  Set foreground color to Blue.
  876. ;Ps = 35  Set foreground color to Magenta.
  877. ;Ps = 36  Set foreground color to Cyan. (Grey?)
  878. ;Ps = 37  Set foreground color to White.
  879. ;Ps = 39  Set foreground color to default, ECMA-48 3rd.
  880. ;Ps = 40  Set background color to Black.
  881. ;Ps = 41  Set background color to Red.
  882. ;Ps = 42  Set background color to Green.
  883. ;Ps = 43  Set background color to Yellow.
  884. ;Ps = 44  Set background color to Blue.
  885. ;Ps = 45  Set background color to Magenta.
  886. ;Ps = 46  Set background color to Cyan.
  887. ;Ps = 47  Set background color to White.
  888. ;Ps = 49  Set background color to default, ECMA-48 3rd.
  889.  ;Ps = 8   Invisible, i.e., hidden, ECMA-48 2nd, VT300. (не работает в Putty!!!)
  890.  ;Ps = 28  Visible, i.e., not hidden, ECMA-48 3rd, VT300. (не работает в Putty!!!)
  891. ;Assume that xterm's resources are set so that the ISO color codes are the first 8 of a set of 16. Then the aixterm colors are the bright versions of the ISO colors:
  892. ;Ps = 90  Set foreground color to Black.
  893. ;Ps = 91  Set foreground color to Red.
  894. ;Ps = 92  Set foreground color to Green.
  895. ;Ps = 93  Set foreground color to Yellow.
  896. ;Ps = 94  Set foreground color to Blue.
  897. ;Ps = 95  Set foreground color to Magenta.
  898. ;Ps = 96  Set foreground color to Cyan.
  899. ;Ps = 97  Set foreground color to White.
  900.  ;TODO 1  -  BRIGHT ON: Включение яркости INK. (Bold, VT100.)
  901.  ;TODO 22 - Normal (neither bold nor faint), ECMA-48 3rd. [21  -  BRIGHT OFF: Выключение яркости INK. (Doubly-underlined, ECMA-48 3rd.)]
  902.         ld a,(pr_textmode_curcolor)
  903.         ld e,a
  904.         ld a,(term_prfsm_curnumber)
  905.         call term_setcolorpp
  906.         ld a,(term_prfsm_curnumber1)
  907.         call term_setcolorpp
  908.         ld a,e
  909.         ld (pr_textmode_curcolor),a
  910.         ret
  911.  
  912. term_prfsm_afterescbracket_scrolldown
  913.         push bc
  914.         ld a,(term_prfsm_curnumber1)
  915.         ld e,a ;xtop
  916.         ld a,(term_prfsm_curnumber)
  917.         ld d,a ;ytop
  918.         ld hl,(term_prfsm_curnumber3) ;первый по счёту
  919.         ld a,(term_prfsm_curnumber2) ;wid
  920.         ld h,a ;hgt
  921.         push de
  922.         push hl
  923.         ld a,l
  924.         cp 80
  925.         jr nz,term_prfsm_afterescbracket_scrolldown_OS
  926. ;de=topyx, hl=hgt,wid
  927. ;x, wid even
  928.         ld a,d
  929.         add a,h
  930.         dec a
  931.         ld d,a ;ybottom
  932.         call BDOS_scroll_prepare
  933. BDOS_scrolldown0
  934.         push bc
  935.         ld d,h
  936.         ld e,l
  937.         ld bc,-64
  938.         add hl,bc
  939.         call BDOS_scrollpageline
  940.         pop bc
  941.         djnz BDOS_scrolldown0
  942.         jr term_prfsm_afterescbracket_scrolldown_OSq
  943. term_prfsm_afterescbracket_scrolldown_OS
  944.         OS_SCROLLDOWN
  945. term_prfsm_afterescbracket_scrolldown_OSq
  946.         pop hl
  947.         pop de        
  948.         ld a,d
  949.         add a,h
  950.         dec a
  951.         ld d,a ;ybottom
  952.         call BDOS_scrollbuf_prepare
  953.        ld (BDOS_scrolldown_call),de
  954. BDOS_scrolldown_buf0
  955.         push bc
  956.         ld d,h
  957.         ld e,l
  958.         dec h
  959. BDOS_scrolldown_call=$+1
  960.         call BDOS_scrollpageline_bufwindow
  961.         pop bc
  962.         djnz BDOS_scrolldown_buf0
  963.         pop bc
  964.         ret
  965.        
  966. cursor_store
  967.         ld hl,(pr_textmode_curaddr)
  968.         ld (cursor_remember_scraddr),hl
  969.         ld hl,(pr_buf_curaddr)
  970.         ld (cursor_remember_bufaddr),hl
  971.         ret
  972. cursor_remember
  973. cursor_remember_scraddr=$+1
  974.         ld hl,0xc1c0
  975.         ld (pr_textmode_curaddr),hl
  976. cursor_remember_bufaddr=$+1
  977.         ld hl,0x4040
  978.         ld (pr_buf_curaddr),hl
  979.         ret
  980.  
  981. term_prfsm_afterescbracket_scrollup
  982.         ld a,(term_prfsm_curnumber1)
  983.         ld e,a ;xtop
  984.         ld a,(term_prfsm_curnumber)
  985.         ld d,a ;ytop
  986.         ld hl,(term_prfsm_curnumber3) ;wid ;первый по счёту
  987.         ld a,(term_prfsm_curnumber2)
  988.         ld h,a ;hgt
  989.         ld a,l
  990.         or a
  991.         jr z,cursor_remember
  992.         push bc
  993.         push de
  994.         push hl
  995.         cp 80
  996.         jr nz,term_prfsm_afterescbracket_scrollup_OS
  997. ;de=topyx, hl=hgt,wid
  998. ;x, wid even
  999.         call BDOS_scroll_prepare
  1000.         call BDOS_scrollpage0 ;62927 t
  1001.         jr term_prfsm_afterescbracket_scrollup_OSq
  1002. term_prfsm_afterescbracket_scrollup_OS
  1003.         OS_SCROLLUP ;95597 t
  1004. term_prfsm_afterescbracket_scrollup_OSq
  1005.         pop hl
  1006.         pop de
  1007.         call BDOS_scrollbuf_prepare
  1008.        ld (BDOS_scrollup_call),de
  1009. BDOS_scrollup_buf0
  1010.         push bc
  1011.         ld d,h
  1012.         ld e,l
  1013.         inc h
  1014. BDOS_scrollup_call=$+1
  1015.         call BDOS_scrollpageline_bufwindow
  1016.         pop bc
  1017.         djnz BDOS_scrollup_buf0 ;62131[91221] t
  1018.         pop bc
  1019.         ret
  1020.  
  1021. BDOS_scrollbuf_prepare
  1022.         ld a,(pgscrbuf)
  1023.         SETPG16K
  1024.         ld a,l
  1025.         srl a
  1026.         ld (BDOS_scrollpagelinelayer_wid),a
  1027.         ld b,h
  1028.         dec b
  1029.          call getscrbuftop_a
  1030.          add a,d ;0..24
  1031.          ld h,a
  1032.          ld a,e
  1033.          or 0x80
  1034.          rrca ;(x/2)+0x40 или 0xc0
  1035.          ld l,a
  1036.         ld a,(BDOS_scrollpagelinelayer_wid)
  1037.         cp 40
  1038.         ld de,BDOS_scrollpageline_buf
  1039.         ret z
  1040.         ld de,BDOS_scrollpageline_bufwindow
  1041.         ret
  1042.  
  1043. BDOS_scroll_prepare
  1044.         BDOSSETPGSSCR
  1045.         ;ld a,l
  1046.         ;srl a
  1047.         ;ld (BDOS_scrollpagelinelayer_wid),a
  1048.         ld b,h
  1049.         dec b
  1050. ;BDOS_countxy
  1051. ;keeps bc
  1052.         ld a,d ;y
  1053.         sub -0x87&0xff ;0xe1c0*4=0x8700
  1054.         rra
  1055.         ld h,a
  1056.          ld a,0;16
  1057.         rra
  1058.         sra h
  1059.         rra
  1060.         ld l,e ;x
  1061.         srl l
  1062.         jr c,$+4
  1063.         res 5,h
  1064.         add a,l
  1065.         ld l,a
  1066.         ret
  1067.  
  1068. BDOS_scrollpageline_bufwindow
  1069.         or a
  1070.         call BDOS_scrollpageline_bufwindowlayers ;text
  1071.         res 6,l ;attr
  1072.         res 6,e ;attr
  1073.         scf
  1074.         call BDOS_scrollpageline_bufwindowlayers ;attr
  1075.         set 6,l ;text
  1076.         ret
  1077. BDOS_scrollpageline_bufwindowlayers
  1078.         push af
  1079.         push de
  1080.         push hl
  1081.         set 7,l
  1082.         set 7,e
  1083.         or a
  1084.         call BDOS_scrollpageline_bufwindowlayer
  1085.         pop hl
  1086.         pop de
  1087.         pop af
  1088. BDOS_scrollpageline_bufwindowlayer
  1089.         push de
  1090.         push hl
  1091.         jr nc,$+4
  1092.         inc hl
  1093.         inc de
  1094. BDOS_scrollpagelinelayer_wid=$+1
  1095.         ld bc,39;40
  1096.         ldir
  1097.         pop hl
  1098.         pop de
  1099.         ret
  1100.  
  1101. BDOS_scrollpageline_buf
  1102.         dup 39
  1103.         ldi
  1104.         edup
  1105.         ld a,(hl)
  1106.         ld (de),a
  1107.         set 7,l
  1108.         set 7,e        
  1109.         dup 39
  1110.         ldd
  1111.         edup
  1112.         ld a,(hl)
  1113.         ld (de),a
  1114.  
  1115.         res 6,l ;attr
  1116.         res 6,e ;attr
  1117.        
  1118.         dup 39
  1119.         ldi
  1120.         edup
  1121.         ld a,(hl)
  1122.         ld (de),a
  1123.         res 7,l
  1124.         res 7,e
  1125.         inc l
  1126.         inc e
  1127.         dup 39
  1128.         ldd
  1129.         edup
  1130.         ld a,(hl)
  1131.         ld (de),a
  1132.  
  1133.         ld l,0x40 ;text
  1134.         ret
  1135.  
  1136. BDOS_scrollpage
  1137. ;156046t [195810t]
  1138.         BDOSSETPGSSCR
  1139.         ld hl,0xc1c0
  1140.         ld b,24
  1141. BDOS_scrollpage0
  1142.         push bc
  1143.         ld d,h
  1144.         ld e,l
  1145.         ld bc,64
  1146.         add hl,bc
  1147.         call BDOS_scrollpageline
  1148.         pop bc
  1149.         djnz BDOS_scrollpage0
  1150.         ret
  1151. BDOS_scrollpageline
  1152.         dup 39
  1153.         ldi
  1154.         edup
  1155.         ld a,(hl)
  1156.         ld (de),a
  1157.         set 5,h
  1158.         set 5,d
  1159.         dup 39
  1160.         ldd
  1161.         edup
  1162.         ld a,(hl)
  1163.         ld (de),a
  1164.        
  1165.         res 6,h ;attr
  1166.         res 6,d ;attr
  1167.        
  1168.         dup 39
  1169.         ldi
  1170.         edup
  1171.         ld a,(hl)
  1172.         ld (de),a
  1173.         res 5,h
  1174.         res 5,d
  1175.         inc l
  1176.         inc e
  1177.         dup 39
  1178.         ldd
  1179.         edup
  1180.         ld a,(hl)
  1181.         ld (de),a
  1182.         dec l
  1183.         dec e
  1184.         set 6,h
  1185.         set 6,d
  1186.         ret
  1187.        
  1188. copylinelayer
  1189.         push de
  1190.         push hl
  1191.         dup 40
  1192.         ldi
  1193.         edup
  1194.         ld a,(hl)
  1195.         ld (de),a
  1196.         pop hl
  1197.         pop de
  1198.         ret
  1199.  
  1200. term_setcolorpp
  1201.         cp 8
  1202.         jr z,term_setinvisible
  1203.         ret c
  1204.         cp 28
  1205.         jr z,term_setvisible
  1206.         cp 90
  1207.         jr c,term_nosetbrightink
  1208.         sub 90-64 ;ink + bright
  1209.         xor e
  1210.         and 0x47
  1211.         xor e
  1212.         ld e,a
  1213.         ret
  1214. term_nosetbrightink
  1215.         cp 40
  1216.         jr c,term_nosetpaper
  1217.         sub 40 ;paper
  1218.         add a,a
  1219.         add a,a
  1220.         add a,a
  1221.         xor e
  1222.         and 0x38
  1223.         xor e
  1224.         ld e,a
  1225.         ret
  1226. term_nosetpaper
  1227.         sub 30 ;ink
  1228.         xor e
  1229.         and 0x47
  1230.         xor e
  1231.         ld e,a
  1232.         ret
  1233. term_setvisible
  1234.         xor a
  1235.         ld (finvisible),a
  1236.         ld (finvisible2),a
  1237.         ret
  1238. term_setinvisible
  1239.         ld a,0x5e ;"ld e,(hl)"
  1240.         ld (finvisible),a
  1241.         ;ld a,0x4e ;"ld c,(hl)"
  1242.         ld (finvisible2),a
  1243.         ret
  1244.  
  1245. MOUSEFACTOR=8
  1246. mousemove
  1247. ;de=mouse delta
  1248. ;чтобы двигать не резко, надо отдельно хранить младшие части x,y (не отображаемые на экране)
  1249.         ld hl,(mousexy)
  1250. htmlcursorxylow=$+1
  1251.         ld bc,0 ;bits 7..5 (for Y) 7..6 (for X), others=0
  1252.         dup 3
  1253.         sla b
  1254.         rl h
  1255.         edup        
  1256.         ld a,h
  1257.         add a,d
  1258.         bit 7,d
  1259.         jr z,html_mousemove_yplus
  1260.         jr nc,html_mousemove_yminus_overflow
  1261.         cp HTMLTOPY*MOUSEFACTOR
  1262.         jr nc,html_mousemove_yq
  1263. html_mousemove_yminus_overflow
  1264.         ld a,HTMLTOPY*MOUSEFACTOR
  1265.         jr html_mousemove_yq
  1266. html_mousemove_yplus
  1267.         jr c,html_mousemove_yplus_overflow
  1268.         cp MOUSEFACTOR*(HTMLTOPY+HTMLHGT-1)
  1269.         jr c,html_mousemove_yq
  1270. html_mousemove_yplus_overflow
  1271.         ld a,MOUSEFACTOR*(HTMLTOPY+HTMLHGT-1)
  1272. html_mousemove_yq  
  1273.         srl a
  1274.         rr b
  1275.         rra
  1276.         rr b
  1277.         rra
  1278.         rr b
  1279.         ld (mousexy+1),a
  1280.  
  1281.         ld h,0
  1282.         sla c
  1283.         rl l
  1284.         sla c
  1285.         adc hl,hl
  1286.         ld a,e
  1287.         rla
  1288.         sbc a,a
  1289.         ld d,a
  1290.         add hl,de
  1291.         bit 7,e
  1292.         jr z,html_mousemove_xplus
  1293.         jr c,html_mousemove_xq
  1294.         ld hl,0 ;ld a,HTMLTOPY*MOUSEFACTOR
  1295.         jr html_mousemove_xq
  1296. html_mousemove_xplus
  1297.         ld de,MOUSEFACTOR/2*(80-1)
  1298.         jr c,html_mousemove_xplus_overflow
  1299.         ;or a
  1300.         sbc hl,de
  1301.         add hl,de
  1302.         jr c,html_mousemove_xq
  1303. html_mousemove_xplus_overflow
  1304.         ex de,hl
  1305. html_mousemove_xq
  1306.         ld a,l
  1307.         rr h
  1308.         rra
  1309.         rr c
  1310.         rra
  1311.         rr c
  1312.         ld (mousexy),a
  1313.  
  1314.         ld (htmlcursorxylow),bc
  1315.         ret
  1316.  
  1317. BDOS_countattraddr_mousecursor
  1318. ;de=yx
  1319.         ld a,(user_scr0_low) ;ok
  1320.         SETPG32KLOW ;attr ;TODO убрать? считывать из scrbuf!
  1321.         ld a,d ;y
  1322.         sub -0x87&0xff ;0xe1c0*4=0x8700
  1323.         rra
  1324.         ld h,a
  1325.          ld a,0;16
  1326.         rra
  1327.         sra h
  1328.         rra
  1329.         ld l,e ;x
  1330.         srl l
  1331.         jr c,$+4
  1332.         res 5,h
  1333.         add a,l
  1334.         ld l,a
  1335.         ld a,h
  1336.         xor 0x60 ;attr + 0x20
  1337.         ld h,a
  1338.          and 0x20
  1339.         ret nz ;jr nz,$+3
  1340.         inc l
  1341.         ret
  1342.  
  1343. BDOS_countattraddr
  1344.         ld a,(user_scr0_low) ;ok
  1345.         SETPG32KLOW ;attr ;TODO убрать? считывать из scrbuf!
  1346.         ld hl,(pr_textmode_curaddr)
  1347. textaddrtoattraddr
  1348.         ld a,h
  1349.         xor 0x60 ;attr + 0x20
  1350.         ld h,a
  1351.          and 0x20
  1352.         ret nz ;jr nz,$+3
  1353.         inc l
  1354.         ret
  1355.  
  1356. getscrbuftop_a
  1357.         ld hl,(pr_buf_curtopaddr)
  1358.         ld a,h
  1359. redraw_scroll=$+1
  1360.         sub 0;24
  1361.          cp 0x40
  1362.          jr nc,$+4
  1363.          ld a,0x40 ;TODO prevpg
  1364.         ret
  1365.  
  1366. cursor_left
  1367. ;TODO с переходом на предыдущую строку
  1368.         ld hl,(pr_textmode_curaddr)
  1369.         ld a,h
  1370.         xor 0x20 ;attr + 0x20
  1371.         ld h,a
  1372.         and 0x20
  1373.         jr z,$+3
  1374.         dec l
  1375.         ld (pr_textmode_curaddr),hl
  1376.         ld hl,(pr_buf_curaddr)
  1377.         ld a,l
  1378.         sub 0x40
  1379.         sub 0x40
  1380.         sbc a,0
  1381.         ld l,a
  1382.         ld (pr_buf_curaddr),hl
  1383.         ret
  1384.  
  1385. cursor_right
  1386.         xor a
  1387.         ld (writed1),a
  1388.         ld (writed2),a
  1389.         ld (writee1),a
  1390.         ld (writee2),a
  1391.         push bc
  1392.         ld a,(term_prfsm_curnumber)
  1393.         sub 1
  1394.         adc a,1 ;0->1
  1395.         ld b,a
  1396. cursor_right0
  1397.         ld a,' '
  1398.         call BDOS_prchar_a_nocrlf
  1399.         djnz cursor_right0
  1400.         pop bc
  1401.         ld a,0x72 ;"ld (hl),d"
  1402.         ld (writed1),a
  1403.         ld (writed2),a
  1404.         ld a,0x73 ;"ld (hl),e"
  1405.         ld (writee1),a
  1406.         ld (writee2),a
  1407.         ret
  1408.  
  1409. BDOS_setx
  1410. ;e=x
  1411.         ld a,e
  1412.         or 0x80
  1413.         rrca ;(x/2)+0x40 или 0xc0
  1414.         ld (pr_buf_curaddr),a
  1415.         ld hl,(pr_textmode_curaddr)
  1416.         ld a,e ;x
  1417.         rra
  1418.         set 5,h
  1419.         jr c,$+4
  1420.         res 5,h
  1421.         xor l
  1422.         and 0x3f
  1423.         xor l
  1424.         ld l,a
  1425.         jr BDOS_settextcuraddr
  1426.  
  1427. BDOS_setxy
  1428. ;de=yx
  1429.          call getscrbuftop_a
  1430.          add a,d ;0..24
  1431.          ld h,a
  1432.          ld a,e
  1433.          or 0x80
  1434.          rrca ;(x/2)+0x40 или 0xc0
  1435.          ld l,a
  1436.          ld (pr_buf_curaddr),hl
  1437.        
  1438.         ld a,d ;y
  1439.         sub -0x87&0xff ;0xe1c0*4=0x8700
  1440.         rra
  1441.         ld h,a
  1442.          ld a,0;16
  1443.         rra
  1444.         sra h
  1445.         rra
  1446.         ld l,e ;x
  1447.         srl l
  1448.         jr c,$+4
  1449.         res 5,h
  1450.         add a,l
  1451.         ld l,a
  1452. BDOS_settextcuraddr
  1453.         ld (pr_textmode_curaddr),hl
  1454.         ret
  1455.        
  1456. BDOS_prchar_controlcode
  1457.          ld hl,(pr_textmode_curaddr)
  1458.         cp 0x0a
  1459.         jr z,BDOS_prchar_lf
  1460.         cp 0x0d
  1461.         jp nz,BDOS_prchar_nocontrolcode
  1462. BDOS_prchar_cr
  1463.          ld a,0x40
  1464.          ld (pr_buf_curaddr),a ;x=0
  1465.         ld a,l
  1466.         and 0xc0
  1467.         ld l,a
  1468.         res 5,h
  1469.         jr BDOS_settextcuraddr
  1470.        
  1471. buftopaddr_down
  1472. pr_buf_curtopaddr=$+1
  1473.         ld hl,0x4040
  1474.         inc h
  1475.         ld (pr_buf_curtopaddr),hl
  1476.         ret
  1477.        
  1478. BDOS_prchar_lf
  1479.         push hl
  1480.         ld hl,(pr_buf_curaddr)
  1481.         inc h
  1482.         call m,scrollscrbuf
  1483.         ld (pr_buf_curaddr),hl
  1484.         ;call buftopaddr_down
  1485.         pop hl
  1486.        
  1487.         ld a,l
  1488.         add a,0x40
  1489.         ld l,a
  1490.         jr nc,BDOS_settextcuraddr
  1491.         jp BDOS_prchar_lf_q
  1492.  
  1493. cursor_down
  1494.         ld a,0x0a ;lf
  1495. BDOS_prchar_a
  1496. ;keeps bc!
  1497.         cp 0x0e
  1498.         jr c,BDOS_prchar_controlcode
  1499. BDOS_prchar_a_nocrlf
  1500. pr_textmode_curaddr=$+1
  1501.         ld hl,0xc1c0
  1502.         ld d,trecode/256
  1503.         ld e,a
  1504.         ld a,(de)
  1505. BDOS_prchar_nocontrolcode
  1506.         ld e,a
  1507. pr_textmode_curcolor=$+1
  1508.         ld d,7
  1509. finvisible=$
  1510.         nop ;/ld e,(hl)
  1511. writee1=$
  1512.         ld (hl),e
  1513.  
  1514.         ld a,h
  1515.         xor 0x60 ;attr + 0x20
  1516.         ld h,a
  1517.         and 0x20
  1518.         jr nz,$+3
  1519.         inc l
  1520. writed1=$
  1521.         ld (hl),d
  1522.  
  1523.         set 6,h
  1524.         ld (pr_textmode_curaddr),hl
  1525.  
  1526. ;scrbuf состоит из строк длиной 256 байт
  1527. ;каждая из них из 4 слоёв:
  1528. ;+0x40: аналог +0x4000 (text0)
  1529. ;+0x80: аналог +0x2000 (attr0)
  1530. ;+0xc0: аналог +0x6000 (text1)
  1531. ;+0x01: аналог +0x0001 (attr1)
  1532.  
  1533. pr_buf_curaddr=$+1
  1534.         ld hl,0x4000+0x40 ;text0
  1535. finvisible2=$
  1536.         nop ;/ld e,(hl)
  1537. writee2=$
  1538.          ld (hl),e
  1539.         ld a,l
  1540.         add a,0x40 ;attr
  1541.         adc a,0
  1542.         ld l,a
  1543. writed2=$
  1544.          ld (hl),d
  1545.         add a,0x40 ;text (next)
  1546.         ld l,a
  1547.         ld (pr_buf_curaddr),hl
  1548.  
  1549.         cp 0x40+(80/2)
  1550.         ret nz ;нет переноса строки
  1551.         ld l,0x40
  1552.         inc h
  1553.         call m,scrollscrbuf
  1554.         ld (pr_buf_curaddr),hl
  1555.         ;call buftopaddr_down
  1556.        
  1557.         ld hl,(pr_textmode_curaddr)
  1558.         ld a,l
  1559.         and 0xc0
  1560.         add a,0x40
  1561.         ld l,a
  1562.         jp nc,BDOS_settextcuraddr
  1563. BDOS_prchar_lf_q
  1564.         inc h
  1565.         bit 3,h
  1566.         jp z,BDOS_settextcuraddr ;нет выхода за последнюю строку
  1567. BDOS_scrolllock0
  1568.         ld a,0xfe
  1569.         in a,(0xfe)
  1570.         rra ;Caps Shift
  1571.         jr nc,BDOS_scrolllock0
  1572. ;scroll+clear bottom line
  1573.        push bc
  1574.         call buftopaddr_down
  1575.         call BDOS_scrollpage ;attr
  1576.         ld a,(user_scr0_high) ;ok ;pgscr0_1 ;text
  1577.         SETPG32KHIGH ;call sys_setpgc000
  1578.         xor a
  1579.         call BDOS_cllastline
  1580.         ld a,(user_scr0_low) ;ok ;pgscr0_0 ;attr
  1581.         SETPG32KHIGH ;call sys_setpgc000
  1582.         ld a,COLOR
  1583.         call BDOS_cllastline
  1584.         BDOSSETPGSSCR
  1585.        pop bc
  1586.         ld hl,0xc7c0
  1587.         jp BDOS_settextcuraddr
  1588.        
  1589. scrollscrbuf
  1590. ;TODO reserve page
  1591. ;TODO unreserve very old page
  1592.        push bc
  1593.         dec h
  1594.         push hl
  1595.         ld de,0x4000 ;0x4040 ;text ;TODO scroll attr
  1596.         ld hx,0x3f*4;*2
  1597. scrollscrbuf0
  1598.         ld h,d
  1599.         ld l,e
  1600.         inc h
  1601.         dup 40
  1602.         ldi
  1603.         edup
  1604.         ld a,(hl)
  1605.         ld (de),a
  1606.         ld bc,64-40;128-40
  1607.         add hl,bc
  1608.         ex de,hl
  1609.         add hl,bc
  1610.         ex de,hl
  1611.         dec hx
  1612.         jp nz,scrollscrbuf0
  1613.         xor a
  1614.         ld hl,0x7f40 ;text
  1615.         call BDOS_scrollpage_cllinelayer
  1616.         ld hl,0x7fc0 ;text
  1617.         call BDOS_scrollpage_cllinelayer
  1618.         ld a,COLOR
  1619.         ld hl,0x7f01 ;attr
  1620.         call BDOS_scrollpage_cllinelayer
  1621.         ld hl,0x7f80 ;attr
  1622.         call BDOS_scrollpage_cllinelayer
  1623.         ld hl,pr_buf_curtopaddr+1
  1624.         dec (hl) ;compensate shift
  1625.         pop hl
  1626.        pop bc
  1627.         ret
  1628.  
  1629. BDOS_cllastline
  1630.         ld hl,0xc7c0
  1631.         call BDOS_scrollpage_cllinelayer
  1632.         ld hl,0xe7c0
  1633. BDOS_scrollpage_cllinelayer
  1634.         ld d,h
  1635.         ld e,l
  1636.         inc e
  1637.         ld bc,41-1;64-1
  1638.         ld (hl),a
  1639.         ldir ;clear bottom line
  1640.         ret
  1641.  
  1642. BDOS_cls
  1643. ;e=color byte
  1644.         ;ld a,(pgscrbuf_low)
  1645.         ;SETPG32KLOW
  1646.         ;ld a,(pgscrbuf_high)
  1647.         ;SETPG32KHIGH
  1648.         ;call clspp
  1649.         ld a,(pgscrbuf)
  1650.         SETPG16K
  1651.         ld hl,0x4000
  1652.         ld de,0x4001
  1653.         ld bc,0x0040
  1654.         ld (hl),COLOR
  1655.         ldir
  1656.         ld c,0x40
  1657.         ld (hl),b;0
  1658.         ldir
  1659.         ld c,0x40
  1660.         ld (hl),COLOR
  1661.         ldir
  1662.         ld c,0x40
  1663.         ld (hl),b;0
  1664.         ldir        
  1665.         dec h;ld hl,0x4000
  1666.         ld de,0x4100
  1667.         ld b,0x3f
  1668.         ldir
  1669. ;TODO reset current number of lines in buf
  1670.  
  1671.         BDOSSETPGSSCR
  1672.  
  1673. ;textmode (6)
  1674. clspp
  1675.         ld a,e
  1676.          ld hl,0x8000
  1677.          call cls_halfpg
  1678.          ld hl,0xa000
  1679.          call cls_halfpg
  1680.  
  1681.         xor a
  1682.          ld hl,0xc000
  1683.          call cls_halfpg
  1684.          ld hl,0xe000
  1685. cls_halfpg
  1686.          ld bc,0x1aff
  1687.         ld d,h
  1688.         ld e,l
  1689.         inc de
  1690.         ld (hl),a
  1691.         ldir
  1692.         ret
  1693.  
  1694.  
  1695. readapp
  1696.         ld a,b
  1697.         ld (curhandle),a
  1698.        
  1699.         OS_NEWAPP ;для первой создаваемой задачи будут созданы первые два пайпа и подключены
  1700. ;dehl=номера страниц в 0000,4000,8000,c000 нового приложения, b=id, a=error
  1701.         push bc ;b=id
  1702.  
  1703.         ld a,d
  1704.         SETPG32KHIGH
  1705.         push de
  1706.         push hl
  1707.         ld hl,COMMANDLINE ;command line
  1708.         call skipword
  1709.         call skipspaces ;пропустили первое слово (там было term.com, а дальше, например, cmd.com autoexec.bat)
  1710.         ld de,0xc080
  1711.         ld bc,128  
  1712.         ldir ;command line
  1713.         pop hl
  1714.         pop de
  1715.  
  1716.         call readfile_pages_dehl
  1717.  
  1718.         ld a,(curhandle)
  1719.         ld b,a
  1720.         OS_CLOSEHANDLE
  1721.  
  1722.         pop af ;id
  1723.         ret
  1724.  
  1725. readfile_pages_dehl
  1726.         ld a,d
  1727.         SETPG32KHIGH
  1728.         ld a,0xc100/256
  1729.         call cmd_loadpage
  1730.         ret nz
  1731.         ld a,e
  1732.         call cmd_loadfullpage
  1733.         ret nz
  1734.         ld a,h
  1735.         call cmd_loadfullpage
  1736.         ret nz
  1737.         ld a,l
  1738. cmd_loadfullpage
  1739.         SETPG32KHIGH
  1740.         ld a,0xc000/256
  1741. cmd_loadpage
  1742. ;out: a=error
  1743. ;keeps hl,de
  1744.         push de
  1745.         push hl
  1746.         ld d,a
  1747.         xor a
  1748.         ld l,a
  1749.         ld e,a
  1750.         sub d
  1751.         ld h,a ;de=buffer, hl=size
  1752. curhandle=$+1
  1753.         ld b,0
  1754.         OS_READHANDLE
  1755.         pop hl
  1756.         pop de
  1757.         or a
  1758.         ret
  1759.  
  1760. skipword
  1761. ;hl=string
  1762. ;out: hl=terminator/space addr
  1763. skipword0
  1764.         ld a,(hl)
  1765.         or a
  1766.         ret z
  1767.         sub ' '
  1768.         ret z
  1769.         inc hl ;ldi
  1770.         jr skipword0
  1771.  
  1772. skipspaces
  1773. ;hl=string
  1774. ;out: hl=after last space
  1775.         ld a,(hl)
  1776.         cp ' '
  1777.         ret nz
  1778.         inc hl
  1779.         jr skipspaces
  1780.  
  1781. ansipal
  1782.         ;dw 0xffff,0xfefe,0xfdfd,0xfcfc,0xefef,0xeeee,0xeded,0xecec
  1783.         ;dw 0x1f1f,0x1e1e,0x1d1d,0x1c1c,0x0f0f,0x0e0e,0x0d0d,0x0c0c
  1784.         dw 0xffff,0xfdfd,0xefef,0xeded,0xfefe,0xfcfc,0xeeee,0xecec
  1785.         dw 0x1f1f,0x1d1d,0x0f0f,0x0d0d,0x1e1e,0x1c1c,0x0e0e,0x0c0c
  1786.  
  1787. term_prfsm_curnumber
  1788.          db 0
  1789. term_prfsm_curnumber1
  1790.          db 0
  1791. term_prfsm_curnumber2
  1792.          db 0
  1793. term_prfsm_curnumber3
  1794.          db 0
  1795.  
  1796. cmd_filename
  1797.         db "cmd.com",0
  1798.  
  1799. tpastaname
  1800.         db "pasta.txt",0
  1801.  
  1802. tpipename
  1803.         db "z:",0
  1804.  
  1805. killbuf_byte
  1806.         db COLOR;0
  1807.  
  1808. pastabuf    
  1809.         db 0
  1810.  
  1811. readpastabuf
  1812.         ds READPASTABUF_SZ
  1813.  
  1814. stdoutbuf
  1815.         ds 6
  1816.  
  1817. stdinbuf
  1818.         ds STDINBUF_SZ
  1819.  
  1820.         align 256
  1821. trecode
  1822.         incbin "../_sdk/codepage/866toatm"
  1823. trecodeback
  1824.         ds 256
  1825.        
  1826. end
  1827.         savebin "term.com",begin,end-begin
  1828.        
  1829.         LABELSLIST "..\..\us\user.l"
  1830.