Subversion Repositories NedoOS

Rev

Rev 660 | Rev 751 | 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.          ex de,hl
  758.          add hl,bc ;dataaddr+byteswritten
  759.          ex de,hl
  760. ;hl=remaining data size
  761. ;de=remaining data addr
  762.         push ix
  763.         push de
  764.         push hl
  765.         YIELDKEEP ;2158t
  766.         call type_stdin
  767.         pop hl
  768.         pop de
  769.         pop bc
  770.         djnz sendchars0
  771.         xor a ;z=no error
  772.         ret ;клиент завис, но не сдох
  773.  
  774. term_prfsm_letter
  775.         ;ld hl,term_prfsm_curstate
  776.         ;ld (hl),TERM_ST_SINGLE
  777.          ;ld b,TERM_ST_SINGLE
  778.         cp 'H'
  779.         jr z,term_prfsm_afterescbracket_H
  780.         cp 'G'
  781.         jr z,term_prfsm_afterescbracket_G
  782.         cp 'm'
  783.         jr z,term_prfsm_afterescbracket_m
  784.         cp 'K'
  785.         jr z,term_prfsm_afterescbracket_clearline
  786. ;TODO J etc.
  787.         ;cp 'A' ;cursor_up
  788.         cp 'B'
  789.         jp z,cursor_down
  790.         cp 'C'
  791.         jp z,cursor_right
  792.         cp 'D'
  793.         jp z,cursor_left
  794.         cp 's'
  795.         jp z,cursor_store
  796.         cp 'd' ;NON-STANDARD!
  797.         jp z,term_prfsm_afterescbracket_scrolldown
  798.         cp 'u' ;NON-STANDARD!
  799.         jp z,term_prfsm_afterescbracket_scrollup ;+cursor remember
  800.         ;cp '~'
  801.         ;jr z,term_prfsm_afterescbracket_tilde
  802.         ret
  803. ;term_prfsm_afterescbracket_tilde
  804. ;        ret
  805.  
  806. term_prfsm_afterescbracket_clearline
  807. ;не двигает курсор
  808.         push bc
  809.         ld de,(pr_textmode_curaddr)
  810.         ld hl,(pr_buf_curaddr)
  811.         ld bc,0x0040
  812. term_prfsm_afterescbracket_clearline0
  813.         xor a
  814.         ld (de),a
  815.         ld a,d
  816.         xor 0x60 ;attr + 0x20
  817.         ld d,a
  818.         and 0x20
  819.         jr nz,$+3
  820.         inc e
  821.         xor a
  822.         ld (de),a
  823.         set 6,d
  824.         ld (hl),a
  825.         ld a,l
  826.         add a,c;0x40 ;attr
  827.         adc a,b;0
  828.         ld l,a
  829.         ld (hl),b;0
  830.         add a,c;0x40 ;text (next)
  831.         ld l,a
  832.         cp 0x40+(80/2)
  833.         jp nz,term_prfsm_afterescbracket_clearline0
  834.         pop bc
  835.         ret
  836.  
  837. term_prfsm_afterescbracket_G
  838.         ld a,(term_prfsm_curnumber) ;column
  839.         dec a
  840.         ld e,a
  841.         jp BDOS_setx
  842.  
  843. term_prfsm_afterescbracket_H
  844.         ld a,(term_prfsm_curnumber1) ;row
  845.         dec a
  846.         ld d,a
  847.         ld a,(term_prfsm_curnumber) ;column
  848.         dec a
  849.         ld e,a
  850.         jp BDOS_setxy
  851.        
  852.         if 1==0
  853.         ;jp forcereprintcursor ;не прокатит? в начале печати cmd тоже setxy
  854. forcereprintcursor
  855.         ;push de
  856.         ;push hl
  857.         ;OS_GETTIMER ;hlde=timer
  858.         ;dec d
  859.         ;ld (lastsdtinmsgtimer),de
  860.          ld hl,0;2
  861.          ld (cursortimelimit),hl
  862.         ;pop hl
  863.         ;pop de
  864.         ret
  865.         endif
  866.        
  867. term_prfsm_afterescbracket_m
  868. ;CSI Pm m Character Attributes (SGR)
  869. ;Ps = 30  Set foreground color to Black.
  870. ;Ps = 31  Set foreground color to Red.
  871. ;Ps = 32  Set foreground color to Green.
  872. ;Ps = 33  Set foreground color to Yellow.
  873. ;Ps = 34  Set foreground color to Blue.
  874. ;Ps = 35  Set foreground color to Magenta.
  875. ;Ps = 36  Set foreground color to Cyan. (Grey?)
  876. ;Ps = 37  Set foreground color to White.
  877. ;Ps = 39  Set foreground color to default, ECMA-48 3rd.
  878. ;Ps = 40  Set background color to Black.
  879. ;Ps = 41  Set background color to Red.
  880. ;Ps = 42  Set background color to Green.
  881. ;Ps = 43  Set background color to Yellow.
  882. ;Ps = 44  Set background color to Blue.
  883. ;Ps = 45  Set background color to Magenta.
  884. ;Ps = 46  Set background color to Cyan.
  885. ;Ps = 47  Set background color to White.
  886. ;Ps = 49  Set background color to default, ECMA-48 3rd.
  887.  ;Ps = 8   Invisible, i.e., hidden, ECMA-48 2nd, VT300. (не работает в Putty!!!)
  888.  ;Ps = 28  Visible, i.e., not hidden, ECMA-48 3rd, VT300. (не работает в Putty!!!)
  889. ;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:
  890. ;Ps = 90  Set foreground color to Black.
  891. ;Ps = 91  Set foreground color to Red.
  892. ;Ps = 92  Set foreground color to Green.
  893. ;Ps = 93  Set foreground color to Yellow.
  894. ;Ps = 94  Set foreground color to Blue.
  895. ;Ps = 95  Set foreground color to Magenta.
  896. ;Ps = 96  Set foreground color to Cyan.
  897. ;Ps = 97  Set foreground color to White.
  898.  ;TODO 1  -  BRIGHT ON: Включение яркости INK. (Bold, VT100.)
  899.  ;TODO 22 - Normal (neither bold nor faint), ECMA-48 3rd. [21  -  BRIGHT OFF: Выключение яркости INK. (Doubly-underlined, ECMA-48 3rd.)]
  900.         ld a,(pr_textmode_curcolor)
  901.         ld e,a
  902.         ld a,(term_prfsm_curnumber)
  903.         call term_setcolorpp
  904.         ld a,(term_prfsm_curnumber1)
  905.         call term_setcolorpp
  906.         ld a,e
  907.         ld (pr_textmode_curcolor),a
  908.         ret
  909.  
  910. term_prfsm_afterescbracket_scrolldown
  911.         push bc
  912.         ld a,(term_prfsm_curnumber1)
  913.         ld e,a ;xtop
  914.         ld a,(term_prfsm_curnumber)
  915.         ld d,a ;ytop
  916.         ld hl,(term_prfsm_curnumber3) ;первый по счёту
  917.         ld a,(term_prfsm_curnumber2) ;wid
  918.         ld h,a ;hgt
  919.         push de
  920.         push hl
  921.         ld a,l
  922.         cp 80
  923.         jr nz,term_prfsm_afterescbracket_scrolldown_OS
  924. ;de=topyx, hl=hgt,wid
  925. ;x, wid even
  926.         ld a,d
  927.         add a,h
  928.         dec a
  929.         ld d,a ;ybottom
  930.         call BDOS_scroll_prepare
  931. BDOS_scrolldown0
  932.         push bc
  933.         ld d,h
  934.         ld e,l
  935.         ld bc,-64
  936.         add hl,bc
  937.         call BDOS_scrollpageline
  938.         pop bc
  939.         djnz BDOS_scrolldown0
  940.         jr term_prfsm_afterescbracket_scrolldown_OSq
  941. term_prfsm_afterescbracket_scrolldown_OS
  942.         OS_SCROLLDOWN
  943. term_prfsm_afterescbracket_scrolldown_OSq
  944.         pop hl
  945.         pop de        
  946.         ld a,d
  947.         add a,h
  948.         dec a
  949.         ld d,a ;ybottom
  950.         call BDOS_scrollbuf_prepare
  951.        ld (BDOS_scrolldown_call),de
  952. BDOS_scrolldown_buf0
  953.         push bc
  954.         ld d,h
  955.         ld e,l
  956.         dec h
  957. BDOS_scrolldown_call=$+1
  958.         call BDOS_scrollpageline_bufwindow
  959.         pop bc
  960.         djnz BDOS_scrolldown_buf0
  961.         pop bc
  962.         ret
  963.        
  964. cursor_store
  965.         ld hl,(pr_textmode_curaddr)
  966.         ld (cursor_remember_scraddr),hl
  967.         ld hl,(pr_buf_curaddr)
  968.         ld (cursor_remember_bufaddr),hl
  969.         ret
  970. cursor_remember
  971. cursor_remember_scraddr=$+1
  972.         ld hl,0xc1c0
  973.         ld (pr_textmode_curaddr),hl
  974. cursor_remember_bufaddr=$+1
  975.         ld hl,0x4040
  976.         ld (pr_buf_curaddr),hl
  977.         ret
  978.  
  979. term_prfsm_afterescbracket_scrollup
  980.         push bc
  981.         ld a,(term_prfsm_curnumber1)
  982.         ld e,a ;xtop
  983.         ld a,(term_prfsm_curnumber)
  984.         ld d,a ;ytop
  985.         ld hl,(term_prfsm_curnumber3) ;wid ;первый по счёту
  986.         ld a,(term_prfsm_curnumber2)
  987.         ld h,a ;hgt
  988.         ld a,l
  989.         or a
  990.         jr z,cursor_remember
  991.         push de
  992.         push hl
  993.         cp 80
  994.         jr nz,term_prfsm_afterescbracket_scrollup_OS
  995. ;de=topyx, hl=hgt,wid
  996. ;x, wid even
  997.         call BDOS_scroll_prepare
  998.         call BDOS_scrollpage0 ;62927 t
  999.         jr term_prfsm_afterescbracket_scrollup_OSq
  1000. term_prfsm_afterescbracket_scrollup_OS
  1001.         OS_SCROLLUP ;95597 t
  1002. term_prfsm_afterescbracket_scrollup_OSq
  1003.         pop hl
  1004.         pop de
  1005.         call BDOS_scrollbuf_prepare
  1006.        ld (BDOS_scrollup_call),de
  1007. BDOS_scrollup_buf0
  1008.         push bc
  1009.         ld d,h
  1010.         ld e,l
  1011.         inc h
  1012. BDOS_scrollup_call=$+1
  1013.         call BDOS_scrollpageline_bufwindow
  1014.         pop bc
  1015.         djnz BDOS_scrollup_buf0 ;62131[91221] t
  1016.         pop bc
  1017.         ret
  1018.  
  1019. BDOS_scrollbuf_prepare
  1020.         ld a,(pgscrbuf)
  1021.         SETPG16K
  1022.         ld a,l
  1023.         srl a
  1024.         ld (BDOS_scrollpagelinelayer_wid),a
  1025.         ld b,h
  1026.         dec b
  1027.          call getscrbuftop_a
  1028.          add a,d ;0..24
  1029.          ld h,a
  1030.          ld a,e
  1031.          or 0x80
  1032.          rrca ;(x/2)+0x40 или 0xc0
  1033.          ld l,a
  1034.         ld a,(BDOS_scrollpagelinelayer_wid)
  1035.         cp 40
  1036.         ld de,BDOS_scrollpageline_buf
  1037.         ret z
  1038.         ld de,BDOS_scrollpageline_bufwindow
  1039.         ret
  1040.  
  1041. BDOS_scroll_prepare
  1042.         BDOSSETPGSSCR
  1043.         ;ld a,l
  1044.         ;srl a
  1045.         ;ld (BDOS_scrollpagelinelayer_wid),a
  1046.         ld b,h
  1047.         dec b
  1048. ;BDOS_countxy
  1049. ;keeps bc
  1050.         ld a,d ;y
  1051.         sub -0x87&0xff ;0xe1c0*4=0x8700
  1052.         rra
  1053.         ld h,a
  1054.          ld a,0;16
  1055.         rra
  1056.         sra h
  1057.         rra
  1058.         ld l,e ;x
  1059.         srl l
  1060.         jr c,$+4
  1061.         res 5,h
  1062.         add a,l
  1063.         ld l,a
  1064.         ret
  1065.  
  1066. BDOS_scrollpageline_bufwindow
  1067.         or a
  1068.         call BDOS_scrollpageline_bufwindowlayers ;text
  1069.         res 6,l ;attr
  1070.         res 6,e ;attr
  1071.         scf
  1072.         call BDOS_scrollpageline_bufwindowlayers ;attr
  1073.         set 6,l ;text
  1074.         ret
  1075. BDOS_scrollpageline_bufwindowlayers
  1076.         push af
  1077.         push de
  1078.         push hl
  1079.         set 7,l
  1080.         set 7,e
  1081.         or a
  1082.         call BDOS_scrollpageline_bufwindowlayer
  1083.         pop hl
  1084.         pop de
  1085.         pop af
  1086. BDOS_scrollpageline_bufwindowlayer
  1087.         push de
  1088.         push hl
  1089.         jr nc,$+4
  1090.         inc hl
  1091.         inc de
  1092. BDOS_scrollpagelinelayer_wid=$+1
  1093.         ld bc,39;40
  1094.         ldir
  1095.         pop hl
  1096.         pop de
  1097.         ret
  1098.  
  1099. BDOS_scrollpageline_buf
  1100.         dup 39
  1101.         ldi
  1102.         edup
  1103.         ld a,(hl)
  1104.         ld (de),a
  1105.         set 7,l
  1106.         set 7,e        
  1107.         dup 39
  1108.         ldd
  1109.         edup
  1110.         ld a,(hl)
  1111.         ld (de),a
  1112.  
  1113.         res 6,l ;attr
  1114.         res 6,e ;attr
  1115.        
  1116.         dup 39
  1117.         ldi
  1118.         edup
  1119.         ld a,(hl)
  1120.         ld (de),a
  1121.         res 7,l
  1122.         res 7,e
  1123.         inc l
  1124.         inc e
  1125.         dup 39
  1126.         ldd
  1127.         edup
  1128.         ld a,(hl)
  1129.         ld (de),a
  1130.  
  1131.         ld l,0x40 ;text
  1132.         ret
  1133.  
  1134. BDOS_scrollpage
  1135. ;156046t [195810t]
  1136.         BDOSSETPGSSCR
  1137.         ld hl,0xc1c0
  1138.         ld b,24
  1139. BDOS_scrollpage0
  1140.         push bc
  1141.         ld d,h
  1142.         ld e,l
  1143.         ld bc,64
  1144.         add hl,bc
  1145.         call BDOS_scrollpageline
  1146.         pop bc
  1147.         djnz BDOS_scrollpage0
  1148.         ret
  1149. BDOS_scrollpageline
  1150.         dup 39
  1151.         ldi
  1152.         edup
  1153.         ld a,(hl)
  1154.         ld (de),a
  1155.         set 5,h
  1156.         set 5,d
  1157.         dup 39
  1158.         ldd
  1159.         edup
  1160.         ld a,(hl)
  1161.         ld (de),a
  1162.        
  1163.         res 6,h ;attr
  1164.         res 6,d ;attr
  1165.        
  1166.         dup 39
  1167.         ldi
  1168.         edup
  1169.         ld a,(hl)
  1170.         ld (de),a
  1171.         res 5,h
  1172.         res 5,d
  1173.         inc l
  1174.         inc e
  1175.         dup 39
  1176.         ldd
  1177.         edup
  1178.         ld a,(hl)
  1179.         ld (de),a
  1180.         dec l
  1181.         dec e
  1182.         set 6,h
  1183.         set 6,d
  1184.         ret
  1185.        
  1186. copylinelayer
  1187.         push de
  1188.         push hl
  1189.         dup 40
  1190.         ldi
  1191.         edup
  1192.         ld a,(hl)
  1193.         ld (de),a
  1194.         pop hl
  1195.         pop de
  1196.         ret
  1197.  
  1198. term_setcolorpp
  1199.         cp 8
  1200.         jr z,term_setinvisible
  1201.         ret c
  1202.         cp 28
  1203.         jr z,term_setvisible
  1204.         cp 90
  1205.         jr c,term_nosetbrightink
  1206.         sub 90-64 ;ink + bright
  1207.         xor e
  1208.         and 0x47
  1209.         xor e
  1210.         ld e,a
  1211.         ret
  1212. term_nosetbrightink
  1213.         cp 40
  1214.         jr c,term_nosetpaper
  1215.         sub 40 ;paper
  1216.         add a,a
  1217.         add a,a
  1218.         add a,a
  1219.         xor e
  1220.         and 0x38
  1221.         xor e
  1222.         ld e,a
  1223.         ret
  1224. term_nosetpaper
  1225.         sub 30 ;ink
  1226.         xor e
  1227.         and 0x47
  1228.         xor e
  1229.         ld e,a
  1230.         ret
  1231. term_setvisible
  1232.         xor a
  1233.         ld (finvisible),a
  1234.         ld (finvisible2),a
  1235.         ret
  1236. term_setinvisible
  1237.         ld a,0x5e ;"ld e,(hl)"
  1238.         ld (finvisible),a
  1239.         ;ld a,0x4e ;"ld c,(hl)"
  1240.         ld (finvisible2),a
  1241.         ret
  1242.  
  1243. MOUSEFACTOR=8
  1244. mousemove
  1245. ;de=mouse delta
  1246. ;чтобы двигать не резко, надо отдельно хранить младшие части x,y (не отображаемые на экране)
  1247.         ld hl,(mousexy)
  1248. htmlcursorxylow=$+1
  1249.         ld bc,0 ;bits 7..5 (for Y) 7..6 (for X), others=0
  1250.         dup 3
  1251.         sla b
  1252.         rl h
  1253.         edup        
  1254.         ld a,h
  1255.         add a,d
  1256.         bit 7,d
  1257.         jr z,html_mousemove_yplus
  1258.         jr nc,html_mousemove_yminus_overflow
  1259.         cp HTMLTOPY*MOUSEFACTOR
  1260.         jr nc,html_mousemove_yq
  1261. html_mousemove_yminus_overflow
  1262.         ld a,HTMLTOPY*MOUSEFACTOR
  1263.         jr html_mousemove_yq
  1264. html_mousemove_yplus
  1265.         jr c,html_mousemove_yplus_overflow
  1266.         cp MOUSEFACTOR*(HTMLTOPY+HTMLHGT-1)
  1267.         jr c,html_mousemove_yq
  1268. html_mousemove_yplus_overflow
  1269.         ld a,MOUSEFACTOR*(HTMLTOPY+HTMLHGT-1)
  1270. html_mousemove_yq  
  1271.         srl a
  1272.         rr b
  1273.         rra
  1274.         rr b
  1275.         rra
  1276.         rr b
  1277.         ld (mousexy+1),a
  1278.  
  1279.         ld h,0
  1280.         sla c
  1281.         rl l
  1282.         sla c
  1283.         adc hl,hl
  1284.         ld a,e
  1285.         rla
  1286.         sbc a,a
  1287.         ld d,a
  1288.         add hl,de
  1289.         bit 7,e
  1290.         jr z,html_mousemove_xplus
  1291.         jr c,html_mousemove_xq
  1292.         ld hl,0 ;ld a,HTMLTOPY*MOUSEFACTOR
  1293.         jr html_mousemove_xq
  1294. html_mousemove_xplus
  1295.         ld de,MOUSEFACTOR/2*(80-1)
  1296.         jr c,html_mousemove_xplus_overflow
  1297.         ;or a
  1298.         sbc hl,de
  1299.         add hl,de
  1300.         jr c,html_mousemove_xq
  1301. html_mousemove_xplus_overflow
  1302.         ex de,hl
  1303. html_mousemove_xq
  1304.         ld a,l
  1305.         rr h
  1306.         rra
  1307.         rr c
  1308.         rra
  1309.         rr c
  1310.         ld (mousexy),a
  1311.  
  1312.         ld (htmlcursorxylow),bc
  1313.         ret
  1314.  
  1315. BDOS_countattraddr_mousecursor
  1316. ;de=yx
  1317.         ld a,(user_scr0_low) ;ok
  1318.         SETPG32KLOW ;attr ;TODO убрать? считывать из scrbuf!
  1319.         ld a,d ;y
  1320.         sub -0x87&0xff ;0xe1c0*4=0x8700
  1321.         rra
  1322.         ld h,a
  1323.          ld a,0;16
  1324.         rra
  1325.         sra h
  1326.         rra
  1327.         ld l,e ;x
  1328.         srl l
  1329.         jr c,$+4
  1330.         res 5,h
  1331.         add a,l
  1332.         ld l,a
  1333.         ld a,h
  1334.         xor 0x60 ;attr + 0x20
  1335.         ld h,a
  1336.          and 0x20
  1337.         ret nz ;jr nz,$+3
  1338.         inc l
  1339.         ret
  1340.  
  1341. BDOS_countattraddr
  1342.         ld a,(user_scr0_low) ;ok
  1343.         SETPG32KLOW ;attr ;TODO убрать? считывать из scrbuf!
  1344.         ld hl,(pr_textmode_curaddr)
  1345. textaddrtoattraddr
  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. getscrbuftop_a
  1355.         ld hl,(pr_buf_curtopaddr)
  1356.         ld a,h
  1357. redraw_scroll=$+1
  1358.         sub 0;24
  1359.          cp 0x40
  1360.          jr nc,$+4
  1361.          ld a,0x40 ;TODO prevpg
  1362.         ret
  1363.  
  1364. cursor_left
  1365. ;TODO с переходом на предыдущую строку
  1366.         ld hl,(pr_textmode_curaddr)
  1367.         ld a,h
  1368.         xor 0x20 ;attr + 0x20
  1369.         ld h,a
  1370.         and 0x20
  1371.         jr z,$+3
  1372.         dec l
  1373.         ld (pr_textmode_curaddr),hl
  1374.         ld hl,(pr_buf_curaddr)
  1375.         ld a,l
  1376.         sub 0x40
  1377.         sub 0x40
  1378.         sbc a,0
  1379.         ld l,a
  1380.         ld (pr_buf_curaddr),hl
  1381.         ret
  1382.  
  1383. cursor_right
  1384.         xor a
  1385.         ld (writed1),a
  1386.         ld (writed2),a
  1387.         ld (writee1),a
  1388.         ld (writee2),a
  1389.         push bc
  1390.         ld a,(term_prfsm_curnumber)
  1391.         sub 1
  1392.         adc a,1 ;0->1
  1393.         ld b,a
  1394. cursor_right0
  1395.         ld a,' '
  1396.         call BDOS_prchar_a_nocrlf
  1397.         djnz cursor_right0
  1398.         pop bc
  1399.         ld a,0x72 ;"ld (hl),d"
  1400.         ld (writed1),a
  1401.         ld (writed2),a
  1402.         ld a,0x73 ;"ld (hl),e"
  1403.         ld (writee1),a
  1404.         ld (writee2),a
  1405.         ret
  1406.  
  1407. BDOS_setx
  1408. ;e=x
  1409.         ld a,e
  1410.         or 0x80
  1411.         rrca ;(x/2)+0x40 или 0xc0
  1412.         ld (pr_buf_curaddr),a
  1413.         ld hl,(pr_textmode_curaddr)
  1414.         ld a,e ;x
  1415.         rra
  1416.         set 5,h
  1417.         jr c,$+4
  1418.         res 5,h
  1419.         xor l
  1420.         and 0x3f
  1421.         xor l
  1422.         ld l,a
  1423.         jr BDOS_settextcuraddr
  1424.  
  1425. BDOS_setxy
  1426. ;de=yx
  1427.          call getscrbuftop_a
  1428.          add a,d ;0..24
  1429.          ld h,a
  1430.          ld a,e
  1431.          or 0x80
  1432.          rrca ;(x/2)+0x40 или 0xc0
  1433.          ld l,a
  1434.          ld (pr_buf_curaddr),hl
  1435.        
  1436.         ld a,d ;y
  1437.         sub -0x87&0xff ;0xe1c0*4=0x8700
  1438.         rra
  1439.         ld h,a
  1440.          ld a,0;16
  1441.         rra
  1442.         sra h
  1443.         rra
  1444.         ld l,e ;x
  1445.         srl l
  1446.         jr c,$+4
  1447.         res 5,h
  1448.         add a,l
  1449.         ld l,a
  1450. BDOS_settextcuraddr
  1451.         ld (pr_textmode_curaddr),hl
  1452.         ret
  1453.        
  1454. BDOS_prchar_controlcode
  1455.          ld hl,(pr_textmode_curaddr)
  1456.         cp 0x0a
  1457.         jr z,BDOS_prchar_lf
  1458.         cp 0x0d
  1459.         jp nz,BDOS_prchar_nocontrolcode
  1460. BDOS_prchar_cr
  1461.          ld a,0x40
  1462.          ld (pr_buf_curaddr),a ;x=0
  1463.         ld a,l
  1464.         and 0xc0
  1465.         ld l,a
  1466.         res 5,h
  1467.         jr BDOS_settextcuraddr
  1468.        
  1469. buftopaddr_down
  1470. pr_buf_curtopaddr=$+1
  1471.         ld hl,0x4040
  1472.         inc h
  1473.         ld (pr_buf_curtopaddr),hl
  1474.         ret
  1475.        
  1476. BDOS_prchar_lf
  1477.         push hl
  1478.         ld hl,(pr_buf_curaddr)
  1479.         inc h
  1480.         call m,scrollscrbuf
  1481.         ld (pr_buf_curaddr),hl
  1482.         ;call buftopaddr_down
  1483.         pop hl
  1484.        
  1485.         ld a,l
  1486.         add a,0x40
  1487.         ld l,a
  1488.         jr nc,BDOS_settextcuraddr
  1489.         jp BDOS_prchar_lf_q
  1490.  
  1491. cursor_down
  1492.         ld a,0x0a ;lf
  1493. BDOS_prchar_a
  1494. ;keeps bc!
  1495.         cp 0x0e
  1496.         jr c,BDOS_prchar_controlcode
  1497. BDOS_prchar_a_nocrlf
  1498. pr_textmode_curaddr=$+1
  1499.         ld hl,0xc1c0
  1500.         ld d,trecode/256
  1501.         ld e,a
  1502.         ld a,(de)
  1503. BDOS_prchar_nocontrolcode
  1504.         ld e,a
  1505. pr_textmode_curcolor=$+1
  1506.         ld d,7
  1507. finvisible=$
  1508.         nop ;/ld e,(hl)
  1509. writee1=$
  1510.         ld (hl),e
  1511.  
  1512.         ld a,h
  1513.         xor 0x60 ;attr + 0x20
  1514.         ld h,a
  1515.         and 0x20
  1516.         jr nz,$+3
  1517.         inc l
  1518. writed1=$
  1519.         ld (hl),d
  1520.  
  1521.         set 6,h
  1522.         ld (pr_textmode_curaddr),hl
  1523.  
  1524. ;scrbuf состоит из строк длиной 256 байт
  1525. ;каждая из них из 4 слоёв:
  1526. ;+0x40: аналог +0x4000 (text0)
  1527. ;+0x80: аналог +0x2000 (attr0)
  1528. ;+0xc0: аналог +0x6000 (text1)
  1529. ;+0x01: аналог +0x0001 (attr1)
  1530.  
  1531. pr_buf_curaddr=$+1
  1532.         ld hl,0x4000+0x40 ;text0
  1533. finvisible2=$
  1534.         nop ;/ld e,(hl)
  1535. writee2=$
  1536.          ld (hl),e
  1537.         ld a,l
  1538.         add a,0x40 ;attr
  1539.         adc a,0
  1540.         ld l,a
  1541. writed2=$
  1542.          ld (hl),d
  1543.         add a,0x40 ;text (next)
  1544.         ld l,a
  1545.         ld (pr_buf_curaddr),hl
  1546.  
  1547.         cp 0x40+(80/2)
  1548.         ret nz ;нет переноса строки
  1549.         ld l,0x40
  1550.         inc h
  1551.         call m,scrollscrbuf
  1552.         ld (pr_buf_curaddr),hl
  1553.         ;call buftopaddr_down
  1554.        
  1555.         ld hl,(pr_textmode_curaddr)
  1556.         ld a,l
  1557.         and 0xc0
  1558.         add a,0x40
  1559.         ld l,a
  1560.         jp nc,BDOS_settextcuraddr
  1561. BDOS_prchar_lf_q
  1562.         inc h
  1563.         bit 3,h
  1564.         jp z,BDOS_settextcuraddr ;нет выхода за последнюю строку
  1565. BDOS_scrolllock0
  1566.         ld a,0xfe
  1567.         in a,(0xfe)
  1568.         rra ;Caps Shift
  1569.         jr nc,BDOS_scrolllock0
  1570. ;scroll+clear bottom line
  1571.        push bc
  1572.         call buftopaddr_down
  1573.         call BDOS_scrollpage ;attr
  1574.         ld a,(user_scr0_high) ;ok ;pgscr0_1 ;text
  1575.         SETPG32KHIGH ;call sys_setpgc000
  1576.         xor a
  1577.         call BDOS_cllastline
  1578.         ld a,(user_scr0_low) ;ok ;pgscr0_0 ;attr
  1579.         SETPG32KHIGH ;call sys_setpgc000
  1580.         ld a,COLOR
  1581.         call BDOS_cllastline
  1582.         BDOSSETPGSSCR
  1583.        pop bc
  1584.         ld hl,0xc7c0
  1585.         jp BDOS_settextcuraddr
  1586.        
  1587. scrollscrbuf
  1588. ;TODO reserve page
  1589. ;TODO unreserve very old page
  1590.        push bc
  1591.         dec h
  1592.         push hl
  1593.         ld de,0x4000 ;0x4040 ;text ;TODO scroll attr
  1594.         ld hx,0x3f*4;*2
  1595. scrollscrbuf0
  1596.         ld h,d
  1597.         ld l,e
  1598.         inc h
  1599.         dup 40
  1600.         ldi
  1601.         edup
  1602.         ld a,(hl)
  1603.         ld (de),a
  1604.         ld bc,64-40;128-40
  1605.         add hl,bc
  1606.         ex de,hl
  1607.         add hl,bc
  1608.         ex de,hl
  1609.         dec hx
  1610.         jp nz,scrollscrbuf0
  1611.         xor a
  1612.         ld hl,0x7f40 ;text
  1613.         call BDOS_scrollpage_cllinelayer
  1614.         ld hl,0x7fc0 ;text
  1615.         call BDOS_scrollpage_cllinelayer
  1616.         ld a,COLOR
  1617.         ld hl,0x7f01 ;attr
  1618.         call BDOS_scrollpage_cllinelayer
  1619.         ld hl,0x7f80 ;attr
  1620.         call BDOS_scrollpage_cllinelayer
  1621.         ld hl,pr_buf_curtopaddr+1
  1622.         dec (hl) ;compensate shift
  1623.         pop hl
  1624.        pop bc
  1625.         ret
  1626.  
  1627. BDOS_cllastline
  1628.         ld hl,0xc7c0
  1629.         call BDOS_scrollpage_cllinelayer
  1630.         ld hl,0xe7c0
  1631. BDOS_scrollpage_cllinelayer
  1632.         ld d,h
  1633.         ld e,l
  1634.         inc e
  1635.         ld bc,41-1;64-1
  1636.         ld (hl),a
  1637.         ldir ;clear bottom line
  1638.         ret
  1639.  
  1640. BDOS_cls
  1641. ;e=color byte
  1642.         ;ld a,(pgscrbuf_low)
  1643.         ;SETPG32KLOW
  1644.         ;ld a,(pgscrbuf_high)
  1645.         ;SETPG32KHIGH
  1646.         ;call clspp
  1647.         ld a,(pgscrbuf)
  1648.         SETPG16K
  1649.         ld hl,0x4000
  1650.         ld de,0x4001
  1651.         ld bc,0x0040
  1652.         ld (hl),COLOR
  1653.         ldir
  1654.         ld c,0x40
  1655.         ld (hl),b;0
  1656.         ldir
  1657.         ld c,0x40
  1658.         ld (hl),COLOR
  1659.         ldir
  1660.         ld c,0x40
  1661.         ld (hl),b;0
  1662.         ldir        
  1663.         dec h;ld hl,0x4000
  1664.         ld de,0x4100
  1665.         ld b,0x3f
  1666.         ldir
  1667. ;TODO reset current number of lines in buf
  1668.  
  1669.         BDOSSETPGSSCR
  1670.  
  1671. ;textmode (6)
  1672. clspp
  1673.         ld a,e
  1674.          ld hl,0x8000
  1675.          call cls_halfpg
  1676.          ld hl,0xa000
  1677.          call cls_halfpg
  1678.  
  1679.         xor a
  1680.          ld hl,0xc000
  1681.          call cls_halfpg
  1682.          ld hl,0xe000
  1683. cls_halfpg
  1684.          ld bc,0x1aff
  1685.         ld d,h
  1686.         ld e,l
  1687.         inc de
  1688.         ld (hl),a
  1689.         ldir
  1690.         ret
  1691.  
  1692.  
  1693. readapp
  1694.         ld a,b
  1695.         ld (curhandle),a
  1696.        
  1697.         OS_NEWAPP ;для первой создаваемой задачи будут созданы первые два пайпа и подключены
  1698. ;dehl=номера страниц в 0000,4000,8000,c000 нового приложения, b=id, a=error
  1699.         push bc ;b=id
  1700.  
  1701.         ld a,d
  1702.         SETPG32KHIGH
  1703.         push de
  1704.         push hl
  1705.         ld hl,COMMANDLINE ;command line
  1706.         call skipword
  1707.         call skipspaces ;пропустили первое слово (там было term.com, а дальше, например, cmd.com autoexec.bat)
  1708.         ld de,0xc080
  1709.         ld bc,128  
  1710.         ldir ;command line
  1711.         pop hl
  1712.         pop de
  1713.  
  1714.         call readfile_pages_dehl
  1715.  
  1716.         ld a,(curhandle)
  1717.         ld b,a
  1718.         OS_CLOSEHANDLE
  1719.  
  1720.         pop af ;id
  1721.         ret
  1722.  
  1723. readfile_pages_dehl
  1724.         ld a,d
  1725.         SETPG32KHIGH
  1726.         ld a,0xc100/256
  1727.         call cmd_loadpage
  1728.         ret nz
  1729.         ld a,e
  1730.         call cmd_loadfullpage
  1731.         ret nz
  1732.         ld a,h
  1733.         call cmd_loadfullpage
  1734.         ret nz
  1735.         ld a,l
  1736. cmd_loadfullpage
  1737.         SETPG32KHIGH
  1738.         ld a,0xc000/256
  1739. cmd_loadpage
  1740. ;out: a=error
  1741. ;keeps hl,de
  1742.         push de
  1743.         push hl
  1744.         ld d,a
  1745.         xor a
  1746.         ld l,a
  1747.         ld e,a
  1748.         sub d
  1749.         ld h,a ;de=buffer, hl=size
  1750. curhandle=$+1
  1751.         ld b,0
  1752.         OS_READHANDLE
  1753.         pop hl
  1754.         pop de
  1755.         or a
  1756.         ret
  1757.  
  1758. skipword
  1759. ;hl=string
  1760. ;out: hl=terminator/space addr
  1761. skipword0
  1762.         ld a,(hl)
  1763.         or a
  1764.         ret z
  1765.         sub ' '
  1766.         ret z
  1767.         inc hl ;ldi
  1768.         jr skipword0
  1769.  
  1770. skipspaces
  1771. ;hl=string
  1772. ;out: hl=after last space
  1773.         ld a,(hl)
  1774.         cp ' '
  1775.         ret nz
  1776.         inc hl
  1777.         jr skipspaces
  1778.  
  1779. ansipal
  1780.         ;dw 0xffff,0xfefe,0xfdfd,0xfcfc,0xefef,0xeeee,0xeded,0xecec
  1781.         ;dw 0x1f1f,0x1e1e,0x1d1d,0x1c1c,0x0f0f,0x0e0e,0x0d0d,0x0c0c
  1782.         dw 0xffff,0xfdfd,0xefef,0xeded,0xfefe,0xfcfc,0xeeee,0xecec
  1783.         dw 0x1f1f,0x1d1d,0x0f0f,0x0d0d,0x1e1e,0x1c1c,0x0e0e,0x0c0c
  1784.  
  1785. term_prfsm_curnumber
  1786.          db 0
  1787. term_prfsm_curnumber1
  1788.          db 0
  1789. term_prfsm_curnumber2
  1790.          db 0
  1791. term_prfsm_curnumber3
  1792.          db 0
  1793.  
  1794. cmd_filename
  1795.         db "cmd.com",0
  1796.  
  1797. tpastaname
  1798.         db "pasta.txt",0
  1799.  
  1800. tpipename
  1801.         db "z:",0
  1802.  
  1803. killbuf_byte
  1804.         db COLOR;0
  1805.  
  1806. pastabuf    
  1807.         db 0
  1808.  
  1809. readpastabuf
  1810.         ds READPASTABUF_SZ
  1811.  
  1812. stdoutbuf
  1813.         ds 6
  1814.  
  1815. stdinbuf
  1816.         ds STDINBUF_SZ
  1817.  
  1818.         align 256
  1819. trecode
  1820.         incbin "../_sdk/codepage/866toatm"
  1821. trecodeback
  1822.         ds 256
  1823.        
  1824. end
  1825.         savebin "term.com",begin,end-begin
  1826.        
  1827.         LABELSLIST "..\..\us\user.l"
  1828.