?login_element?

Subversion Repositories NedoOS

Rev

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