SteveOS for 16 bit processors (286)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

184 lines
3.5 KiB

  1. ;; Steve16 text editor
  2. sedit_init:
  3. pusha
  4. mov cx, 0xFF
  5. mov bx, SEDIT_INIT_MSG ;Init msg
  6. call print
  7. mov [SEDIT_EDITOR_BUFFER], byte 0
  8. ;; clear the screen
  9. call kernel_scn_cls
  10. ;; read in from disk
  11. mov dh, 0
  12. mov cx, 0xD ;sect & cyl
  13. mov al, 2
  14. mov bx, SEDIT_EDITOR_BUFFER
  15. call read_sector
  16. mov cx, 0xFF
  17. ;; print it out
  18. call print
  19. mov [SEDIT_CURSOR_POSITION], byte 0
  20. ;; increment cursor by correct amount
  21. mov bx, SEDIT_EDITOR_BUFFER
  22. sedit_cursor_inc:
  23. cmp [bx], byte 0
  24. je sedit_loop
  25. inc bx
  26. inc byte [SEDIT_CURSOR_POSITION]
  27. jmp sedit_cursor_inc
  28. sedit_loop:
  29. ;; Read text in
  30. mov ah, 0x00
  31. int 0x16 ;Read kb
  32. cmp al, 0x1B ;Esc pressed
  33. je sedit_exit
  34. cmp al, 0xD ;Enter key
  35. je sedit_enter
  36. cmp al, 0x8 ;Backspace
  37. je sedit_backspace
  38. ;; Compare with other special characters
  39. cmp al, ' '
  40. jl sedit_loop ;al is less than space
  41. cmp al, 0x7F
  42. jg sedit_loop ;al is greater than ~
  43. ;; At this point, we know it's a regular character
  44. mov bx, SEDIT_EDITOR_BUFFER
  45. add bx, [SEDIT_CURSOR_POSITION]
  46. mov [bx], al ;Append character to text buffer
  47. mov bx, 0xFF
  48. call print_char ;pretty temporary
  49. ;; Increment cursor position
  50. add [SEDIT_CURSOR_POSITION], byte 1
  51. jmp sedit_loop
  52. sedit_enter:
  53. mov bx, SEDIT_EDITOR_BUFFER
  54. add bx, [SEDIT_CURSOR_POSITION]
  55. mov [bx], byte 0xD
  56. add bx, byte 1
  57. mov [bx], byte 0xA ;Add 0xD and 0xA to buffer
  58. add [SEDIT_CURSOR_POSITION], byte 2 ;Increment pos
  59. mov al, 0xD
  60. call print_char
  61. mov al, 0xA
  62. call print_char ;Print out the enter
  63. jmp sedit_loop
  64. sedit_backspace:
  65. ;; Backspace the teletype
  66. cmp [SEDIT_CURSOR_POSITION], byte 0 ;don't bs if empty buffer
  67. je sedit_loop
  68. ;; remove character from buffer
  69. sub [SEDIT_CURSOR_POSITION], byte 1
  70. mov bh, 0
  71. mov ah, 0x03
  72. int 0x10 ;Get the cursor's position
  73. cmp dl, 0 ;In the first column
  74. jne sedit_skip_upline
  75. ;; At this point, the cursor is at the start of the line
  76. ;; 2 options - either last character is a newline
  77. ;; -> go up a line and as far over as necessary
  78. ;; or last character isn't
  79. ;; -> go up a line, far right
  80. ;; check last char
  81. mov bx, SEDIT_EDITOR_BUFFER
  82. add bx, [SEDIT_CURSOR_POSITION]
  83. dec bx
  84. cmp [bx], byte 0xD
  85. je sedit_backspace_newline
  86. cmp [bx], byte 0xA
  87. je sedit_backspace_newline
  88. mov ah, 0x02
  89. mov dl, 79 ;80 column display
  90. sub dh, 1 ;One row up
  91. int 0x10
  92. mov ah, 0x0E
  93. mov al, ' '
  94. int 0x10 ;Print a space, putting us back where we were before
  95. mov ah, 0x02
  96. int 0x10 ;And back up we go
  97. jmp sedit_loop
  98. sedit_backspace_newline:
  99. ;; TODO: dont assume that there's 2 newline characters(0xD and 0xA)
  100. sub [SEDIT_CURSOR_POSITION], byte 1
  101. call sedit_refresh_screen
  102. jmp sedit_loop
  103. sedit_skip_upline:
  104. ;; bs, space, bs
  105. mov al, 0x8
  106. call print_char
  107. mov al, ' '
  108. call print_char
  109. mov al, 0x8
  110. call print_char
  111. jmp sedit_loop
  112. sedit_refresh_screen: ;redraws screen from buffer
  113. ;; set the video mode, clearing the screen
  114. mov ax, 0x03
  115. int 0x10
  116. mov bx, SEDIT_EDITOR_BUFFER
  117. add bx, [SEDIT_CURSOR_POSITION]
  118. mov [bx], byte 0 ;add a null terminator
  119. mov bx, SEDIT_EDITOR_BUFFER
  120. call print
  121. ret
  122. sedit_exit:
  123. ;; null terminator
  124. mov bx, SEDIT_EDITOR_BUFFER
  125. add bx, [SEDIT_CURSOR_POSITION]
  126. mov [bx], byte 0
  127. ;; save buffer
  128. mov dh, 0
  129. mov cx, 0xD ;sect & cyl
  130. mov al, 2
  131. mov bx, SEDIT_EDITOR_BUFFER
  132. call write_sector
  133. ;; print & exit
  134. mov bx, SEDIT_EXIT_MSG
  135. call print
  136. popa
  137. ret
  138. ;; Varibles
  139. SEDIT_CURSOR_POSITION: dw 0
  140. ;; Messages
  141. SEDIT_INIT_MSG: db "Steve16 Text Editor", 0xD, 0xA, 0
  142. SEDIT_EXIT_MSG: db "Goodbye!", 0xD, 0xA, 0