?login_element?

Subversion Repositories NedoOS

Rev

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

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