-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-editor.asm
1649 lines (1390 loc) · 44.2 KB
/
text-editor.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Provided under the MIT License: http://mit-license.org/
; Copyright (c) 2020 Andy Kallmeyer <ask@ask.systems>
; This is a text editor (uses a gap buffer) which calls the run_code function
; when you press ctrl+D. You use it by %define RUN_CODE before you include the
; text editor, and then define your run_code function after that.
;
; You may also define a debug_text label pointing to a null terminated string
; which will be pre-loaded into the editor on startup if DEBUG_TEXT is defined.
;
; Search for run_code: below to see the API for it.
; Color byte is: bg-intensity,bg-r,bg-g,bg-b ; fg-intensity,fg-r,fg-g,fg-b
%define MAIN_COLOR 0x17
%define BORDER_COLOR 0x97
%define ERROR_COLOR 0x47
; This is a different color space from the others. It's 6 bits.
; secondary r,g,b then primary r,g,b
; So 0x39 is 1 for all secondary values and 1 for b
%define OVERSCAN_COLOR 0x39
; Note that the code requires that there's at least one character of border
%define MAIN_TOP_LEFT 0x0204 ; row = 2, col = 4
%define MAIN_BOTTOM_RIGHT 0x164B ; row = 22, col = 79-4
%define START_ROW 0x02
%define START_COL 0x04
%define END_ROW 0x16
%define END_COL 0x4B
; TODO: detect the RAM and give an error if we don't have 256k which is 0x40000
; which is exactly the 4 segments we use (the 4th is the code segment)
; Segment register value (so the actual start is at the address 0x10*this)
; This is the first sector after the editor's code
;
; This offset won't be exactly a multiple of NEXT_SEGMENT but it is at most
; NEXT_SEGMENT because we don't have a dynamic code segment switching setup so
; NUM_EXTRA_SECTORS is limited to 0x7F. So since there are 6
; non-overlapping complete segments available and we're only using 4 here
; (including the code segment), it's impossible for COMPILER_OUT_LOC to be
; greater than LAST_SEGMENT.
%define USER_CODE_LOC (CODE_SEGMENT+(SECTOR_SIZE/0x10)*(NUM_EXTRA_SECTORS+1))
%define COMPILER_DATA_LOC USER_CODE_LOC+NEXT_SEGMENT
%define COMPILER_OUT_LOC COMPILER_DATA_LOC+NEXT_SEGMENT
; Maxiumum size for the code buffer
;
; There's a lot of extra memory still after this but I don't want to move around
; the segment register so this is the limit.
;
; Allows 64k of code
%define USER_CODE_MAX 0xFFFF
; Values in ax after the keyboard read BIOS call
; See Figure 4-3 of the 1988 BIOS manual. (page 195)
%define LEFT_ARROW 0x4B00
%define RIGHT_ARROW 0x4D00
%define UP_ARROW 0x4800
%define DOWN_ARROW 0x5000
%define EOT 0x2004 ; Ctrl+D
%define ROW_COUNT (END_ROW-START_ROW+1) ; 0x15, 21
%define ROW_LENGTH (END_COL-START_COL+1) ; 0x48, 72
%define NUM_PRINTS_PER_ROW (ROW_LENGTH/5) ; 5 = 4 hex chars + 1 space
; Size of the gap buffer when we reset it
; Must be less than USER_CODE_MAX
;
; The user code is stored using a gap buffer to allow inserting at the cursor
; location without having to shift the trailing data in memory. The system
; maintains a single gap in the code buffer at the cursor position which shrinks
; as the usertypes continuously and must periodically be reset by shifting the
; data. When the user deletes the gap grows. When the cursor hits the end of the
; buffer the gap can be reset for free.
%define GAP_SIZE (8*ROW_LENGTH)
; start_ should be the first bytes in the file because that's where I have
; configured gdb to breakpoint
start_:
; TODO: CGA support
; Set the overscan color to the border color
mov ax, 0x1001
mov bh, OVERSCAN_COLOR
int 0x10
; Set the border color (by clearing the whole screen)
mov ax, 0x0600
xor cx, cx ; row = 0, col = 0
mov dx, 0x184F ; row = 24, col = 79
mov bh, BORDER_COLOR
int 0x10
; Set the background color (by clearing just the middle)
mov ax, 0x0600
mov cx, MAIN_TOP_LEFT
mov dx, MAIN_BOTTOM_RIGHT
mov bh, MAIN_COLOR
int 0x10
; Set cursor position to the start
mov ax, 0x0200
mov dx, MAIN_TOP_LEFT
xor bh, bh ; page 0
int 0x10
; Memory map:
;
; Each segment gets a full non-overlapping 64k block of memory and we don't move
; the segment so 64k is the limit for each part.
;
; SS: 0x050
; - Leaves ~30k for the stack between 0x0500 and 0x7C00, the start of the code.
; CS: CODE_SEGMENT (0x7C0)
; - The assembly code
; ES: USER_CODE_LOC or COMPILER_OUT_LOC if we're printing the output
; - The code that the user types into the editor
; - The printing routines all use es so we use es for printing
; DS: COMPILER_DATA_LOC
; - Extra memory passed to the plugin program that runs the code you typed.
; Only set when ctrl+D is pressed and passed to the plugin.
; - Normally the default for memory address reads and our bootloader leaves
; this set to CODE_SEGMENT for that. But this code always uses cs: to
; reference strings stored in the binary.
; --- typing_loop global register variables ---
;
; The user code is an (almost) contiguous null-terminated buffer starting at
; es:0 with a single gap of garbage data in it (which lets us do inserts without
; shifting the tail of the data every time to make space).
;
; dx - cursor position (set above)
; [es:di] - the first garbage character in the gap (current position to write to)
; [es:si] - the first code character after the gap
;
; Note: We must have at least one char of gap i.e. si-di >= 1 at all times
; (because we use it as scratch space when printing sometimes)
;
; Additionally cx,bp are callee save while ax,bx are caller save (clobbered)
mov ax, USER_CODE_LOC
mov es, ax
mov ax, COMPILER_DATA_LOC
mov ds, ax
xor di, di
mov si, GAP_SIZE
mov byte [es:si], 0 ; null-terminate the string
%ifndef DEBUG_TEXT
jmp typing_loop
%else
; Move the debug_text into the segment used for the text editor code
mov di, debug_text
copy_debug_text:
mov al, [cs:di]
mov [es:si], al
cmp byte [cs:di], 0
je done_copy_debug
inc di
inc si
jmp copy_debug_text
done_copy_debug:
; Print it nicely in the editor
mov si, GAP_SIZE
mov di, GAP_SIZE
mov dh, START_ROW
mov cx, ROW_COUNT
call print_text
; Scroll the printed text to the top corner where the cursor starts
cmp cx, 0
je .skip_scolling
mov bx, cx
mov al, 0
mov dx, MAIN_TOP_LEFT
call scroll_text
.skip_scolling:
; Reset and run the typing_loop
xor di, di
mov si, GAP_SIZE
mov dx, MAIN_TOP_LEFT
jmp set_cursor_and_continue
%endif
; Optionally change the keyboard layout.
;
; To use a layout: look in the file you want for the %ifdef label and -D define
; it on the commandline. Ex: ./boot bootstrap-asm.asm -DDVORAK
;
; It's easy to make a new layout!
;
; Simply pull up an ascii table (my favorite is `man ascii`) then type it out
; starting from ' ' to '~' using your qwerty keyboard labels using the desired
; layout in your OS (note: both \ and ` must be escaped for the assembler)
%include "keyboard-layouts/dvorak.asm"
; TODO: is the default layout qwerty or hardware specific? My map is qwerty->dvorak only
no_more_room_msg: db `No more room in the code buffer`
no_more_room_msg_len: equ $-no_more_room_msg
num_debug_prints: db 0x00
; Prints a hex word in the top margin for debugging purposes
; Supports printing as many as will fit in before it runs into the text area
;
; cx = two bytes to write at current cursor
; clobbers ax, and bx
debug_print_hex:
push dx ; Save the cursor position
; One row above the top of the text area
mov dl, START_COL
xor dh, dh
; Calculate where to print based on number of words we have printed
xor ax, ax
mov byte al, [cs:num_debug_prints]
mov bl, NUM_PRINTS_PER_ROW
div bl
; al = (num_debug_prints)/(num_per_row); ah = (num_debug_prints) % (num_per_row)
add dh, al ; row += the row to print on
; Reset the count so we overwrite at the beginning
cmp dh, START_ROW
jne .no_reset
mov byte [cs:num_debug_prints], 0
xor ax, ax
xor dh, dh
.no_reset:
inc byte [cs:num_debug_prints] ; remember that we printed
; column += modulus * (hex+" " string length)
mov al, ah
xor ah, ah
mov bl, 5
mul bl
add dl, al ; note: the mul result technically could be more than 2^8 (in ax) but we know it is less than ROW_LENGTH
; set cursor to the location to print
mov ah, 0x02
xor bh, bh
int 0x10 ; dx is the cursor position
; Print cx
call print_hex
; don't bother printing a space because the margin is blank
; reset the cursor
pop dx
mov ah, 0x02
xor bh, bh
int 0x10 ; dx is the cursor position
ret
user_code_start: dw 0
; Note: all of the jumps in typing_loop loop back here (except for run_code)
;
; I thought it would be fun to save the call and ret instructions since the
; entire program is just this loop (after the setup).
typing_loop:
; Read keyboard
mov ah, 0x00
int 0x16
; ah = key code, al = ascii value
cmp al, ' '
jb .non_printable
cmp al, '~'
ja .non_printable
jmp save_and_print_char
.non_printable:
cmp al, `\b` ; Backspace, shift backspace, Ctrl+H
je backspace
cmp al, `\r` ; Enter/Return key
je save_new_line
cmp ax, LEFT_ARROW
je move_left
cmp ax, RIGHT_ARROW
je move_right
cmp ax, UP_ARROW
je move_up
cmp ax, DOWN_ARROW
je move_down
cmp ax, EOT ; Ctrl+D
je prepare_and_run_code
jmp typing_loop ; Doesn't match anything above
%ifndef RUN_CODE
; This is run when ctrl+D is pressed and passed the code and memory locations
; for compiler/interpreter purposes and finally a separate output text buffer
; which will be displayed in the editor after run_code returns.
;
; Input:
; [ds:si] - the code to run
; [es:0] - the code runner memory block.
; This memory is preserved between successive runs of run_code.
; di - output segment address; starts at 0 offset; null terminated
; This memory is preserved between successive runs of run_code.
run_code:
mov es, di
mov byte [es:0], "!"
mov byte [es:1], 0
ret
%endif
prepare_and_run_code:
call clear_error ; In case this isn't the first try
; Close the gap so we have a contiguous buffer
.close_gap:
mov byte al, [es:si]
mov byte [es:di], al
; Check for \0 after moving because we want to copy it over
cmp byte [es:si], 0
je .gap_closed
inc di
inc si
jmp .close_gap
.gap_closed:
; Clear the text from the screen
mov dx, MAIN_TOP_LEFT
mov bl, END_COL-START_COL
call scroll_text
; Set the cursor position to the start of the text area
mov ah, 0x02
xor bh, bh
int 0x10
push si
; source code is [es:si], dest data is [ds:di]
; set the source [es:si] for the assembler
mov di, COMPILER_OUT_LOC
mov si, [cs:user_code_start]
call run_code
mov ax, COMPILER_OUT_LOC
mov es, ax
xor si, si
xor di, di ; set es:si to the same as es:di so the gap buffer code works
mov dh, START_ROW
call print_text
; Reset the state to continue the editor
;
; TODO: make an option to save the input text instead of resetting the buffer
; with an empty null terminated string. Not sure what I want the controls to
; be. Would probably want to reset the lisp memory too at that point.
pop si
mov [cs:user_code_start], si
mov di, si
add si, GAP_SIZE
mov byte [es:si], 0 ; null-terminate the string
mov ax, USER_CODE_LOC
mov es, ax
; Read keyboard, so they can read the output
mov ah, 0x00
int 0x16
; Clear the text from the screen
mov dx, MAIN_TOP_LEFT
mov bl, END_COL-START_COL
call scroll_text
mov dx, MAIN_TOP_LEFT
jmp set_cursor_and_continue
; Print a body of text into the text editor respecting newlines and the screen
; boundaries. Always starts printing at the left edge.
;
; Args:
; [es:si] - the null terminated text to print
; cx - max lines to print, 0 for unlimited
; Returns:
; cx - the number of lines remaining in the max
; (or negative lines printed if cx was 0)
; i.e. cx = (input cx) - lines_printed
; So if you set cx non-zero this is the number of lines to scroll up
print_text:
push cx
jmp .skip_scroll
.print_loop:
; scroll up by one line to make room for the next line to print, do this at
; the top so it doesn't happen after the last line
mov al, 0
mov bl, 1
mov dx, MAIN_TOP_LEFT
call scroll_text
.skip_scroll:
; Find the end of the line
mov bp, si
call scan_forward
; Check if we found the end of the line, and set the right scroll marker
mov ax, 0x0000 ; scroll markers: set to off ; right margin
cmp cx, ROW_LENGTH
jbe .shorter_than_row
mov cx, ROW_LENGTH ; only print up to the edge of the screen
mov ax, 0x0100 ; scroll markers: set to on ; right margin
; fallthrough
.shorter_than_row:
; move bp to the end of the next line
cmp byte [es:bp], 0
je .end_of_buffer
inc bp ; skip the \n
; fallthrough
.end_of_buffer:
; ax was set above
mov dh, END_ROW
call set_line_scroll_marker
; swap bp and si
; after: bp = beginning of line, si = start of next line
xor si, bp
xor bp, si
xor si, bp
mov dh, END_ROW
mov dl, START_COL
call print_line ; prints cx chars from bp
; Keep track of the lines printed
pop cx
dec cx
; If we have run out of our line limit, stop
; If cx was 0 for unlimited then this will be negative and not equal to 0
cmp cx, 0
je .end
; If we hit the end of the buffer, stop
cmp byte [es:bp], 0
je .end
; If we're still looping push line_count again so we can pop it above
push cx
jmp .print_loop
.end:
ret
; ==== typing_loop internal helpers that continue the loop ====
; Moves the cursor to the value in dx and continues typing_loop
set_cursor_and_continue:
mov ah, 0x02
xor bh, bh
int 0x10 ; dx is the cursor position
jmp typing_loop
; Takes an ascii char in al then saves and prints it and continues typing_loop
save_and_print_char:
lea bx, [si-1]
cmp di, bx ; disallow typing anywhere when we're out of buffer space
je typing_loop
cmp di, USER_CODE_MAX ; Leave a \0 at the end
je typing_loop
; Convert the keyboard layout if one is %included
%ifdef CONVERT_LAYOUT
call convert_keyboard_layout
%endif
; Print the ascii char (in al)
mov ah, 0x0E ; Write teletype character
xor bh, bh
int 0x10
mov byte [es:di], al ; Write the typed char to the buffer
inc di
call maybe_reset_gap
cmp dl, END_COL
je _scroll_line_right ; same if we're inserting or at the end of the line
; We're inserting in the middle of the row, repaint the tail of the line
inc dl
mov bp, si
call scan_forward
; bx = remaining space in the row from the current cursor position
mov bx, (ROW_LENGTH+START_COL)
sub bl, dl
cmp cx, bx
jbe .not_cut_off
mov cx, bx ; clip the amount to repaint to the current row
mov ax, 0x0100 ; set to on ; right margin
call set_line_scroll_marker
; fallthrough
.not_cut_off:
mov bp, si
call print_line
; The cursor did move from the typing interrupt but lets just set it anyway,
; if we didn't the cursor breaks when putting in a debug print in some places
jmp set_cursor_and_continue
save_new_line:
lea bx, [si-1]
cmp di, bx ; disallow typing anywhere when we're out of buffer space
je typing_loop
cmp di, USER_CODE_MAX ; Leave a \0 at the end
je typing_loop
; Write the `\n` into the buffer
mov byte [es:di], `\n`
inc di
call maybe_reset_gap
; fallthrough
.reset_current_line:
; If we put a \n in the first character, so there's no string to scan
mov bx, [cs:user_code_start]
inc bx
cmp di, bx
ja .not_empty_first_line
; Clear it incase we were scolled. We're not doing a scan_forward to check
mov ax, 0x0000 ; set to off ; right margin
call set_line_scroll_marker
jmp .clear_tail
.not_empty_first_line:
lea bp, [di-2]
call scan_backward
; Set the scroll markers
mov ax, 0x0001 ; set to off ; left margin
call set_line_scroll_marker
; Set right to on or off always
mov ax, 0x0000 ; set to off ; right margin
cmp cx, ROW_LENGTH
jbe .right_off
mov cx, ROW_LENGTH ; also clip the length for printing (reuse the cmp)
mov ax, 0x0100 ; set to on ; right margin
.right_off:
call set_line_scroll_marker
; Repaint the line
mov dl, START_COL
call print_line
add dl, cl ; move the cursor to the end of the string (for .clear_tail)
cmp cx, ROW_LENGTH
je .move_down ; skip clearing the tail if we just repainted everything
; fallthrough
.clear_tail:
cmp byte [es:si], `\n`
je .move_down ; no need to clear if we were at the end of the line already
cmp byte [es:si], 0
je .move_down ; nothing to clear at the end of the buffer
; Write spaces to clear the rest of the line since it's on the next line now
; Note: this call uses the actual set cursor position not dx
mov ah, 0x09
mov al, ' '
xor bh, bh
mov bl, MAIN_COLOR
; cx = 1 + END_COL - curr_cursor_col = num spaces to write
mov cx, END_COL+1
sub cl, dl
int 0x10
; fallthrough
; Do a partial screen scroll to make room for the new line in the middle
.move_down:
mov dl, START_COL ; start at the beginning on the next line
; If there's no text after our tail, we don't want to scroll anything
mov bp, si
call scan_forward ; Note we need to save the return (cx) until .print_new_line
cmp dh, END_ROW
jne .make_space_in_middle
; Move the text one line up to make an empty line at the bottom
mov dh, START_ROW ; scroll the whole screen
mov al, 0
mov bl, 1 ; scroll one line
call scroll_text
mov dh, END_ROW ; restore the cursor row
jmp .print_new_line
.make_space_in_middle:
inc dh
cmp byte [es:bp], 0 ; if this is the last line, we already have space
je .no_scroll
mov al, 1 ; shift the text down to leave a space
mov bl, 1 ; scroll one line
call scroll_text
.no_scroll:
; fallthrough
; Print the tail of the line after the new \n if there is any
; (uses cx from scan_forward above)
.print_new_line:
; re-use the saved result of scan_forward from the check above
cmp cx, ROW_LENGTH
jbe .not_cut_off
mov cx, ROW_LENGTH ; clamp to the row length
mov ax, 0x0100 ; set to on ; right margin
call set_line_scroll_marker
; fallthrough
.not_cut_off:
mov bp, si
call print_line
jmp set_cursor_and_continue
backspace:
cmp di, [cs:user_code_start] ; Can't delete past the beginning of the buffer
je typing_loop
lea bx, [si-1]
cmp di, bx ; disallow typing anywhere when we're out of buffer space
jne .not_full_buffer
call clear_error
; fallthrough
.not_full_buffer:
cmp byte [es:di-1], `\n` ; check the char we just "deleted" (di-1 is usually the char before the cursor)
je _join_lines
; If we're at the beginning of the line we don't have to update the view
cmp dl, START_COL
jne .delete_mid_line
dec di ; delete the unseen char by pushing it into the gap
; Remove the scroll marker if we just hit the beginning of the line
cmp di, [cs:user_code_start]
je .clear_left_marker
cmp byte [es:di-1], `\n`
je .clear_left_marker
jmp typing_loop
.clear_left_marker:
mov ax, 0x0001 ; set to off ; left margin
call set_line_scroll_marker
jmp typing_loop
.delete_mid_line:
dec di ; delete the char by pushing it into the gap
dec dl ; Update the cursor position
; .repaint_tail:
mov bp, si
call scan_forward
; bx = maximum chars until the end of the row
mov bx, (START_COL+ROW_LENGTH)
sub bl, dl
cmp cx, bx
jae .clip_line ; in the equals case we don't want to print an extra space
; We're re-painting a line tail that's not cut off currently. So:
; Replace the line-ending char with a space for covering up the old last char
push word [es:bp-1] ; save the line ending char in the high byte (you can only push whole words)
; Note: [es:bp+1] may not be safe to read while -1 is here.
mov byte [es:bp], ' '
push bp ; save the pointer to the line ending
; Repaint the tail of the line
inc cx ; print the space too
mov bp, si
call print_line
; Restore the line ending
pop bp
pop ax
mov byte [es:bp], ah
jmp set_cursor_and_continue
.clip_line:
; Set the right marker on, unless our line exactly ends at the end of the row
mov ax, 0x0000 ; set to off ; right margin
cmp cx, bx
je .no_marker
mov ax, 0x0100 ; set to on ; right margin
.no_marker:
call set_line_scroll_marker
; Print only up to our maximum (recalculated from above since bx gets clobbered)
mov cx, END_COL+1
sub cl, dl
mov bp, si
call print_line
jmp set_cursor_and_continue
_repaint_bottom_line:
push dx
; Note: the first iteration will just be the tail of the new joined line
mov bp, si
call scan_forward ; skip to the end of the new combined line
.next_line_loop:
cmp dh, END_ROW
je .at_last_line
; If we hit the end of the code before the END_ROW, we didn't have a line cut off
cmp byte [es:bp], 0
je .no_line_to_print
inc bp ; skip the \n to scan the next line
call scan_forward
inc dh
jmp .next_line_loop
.at_last_line:
; Set the right scroll marker
mov ax, 0x0000 ; set to off ; right margin
cmp cx, ROW_LENGTH
jbe .right_off
mov cx, ROW_LENGTH ; also clip the length for printing (reuse the cmp)
mov ax, 0x0100 ; set to on ; right margin
.right_off:
call set_line_scroll_marker
sub bp, cx
call print_line ; if we hit the end row, paint the new last screen line
; fallthrough
.no_line_to_print:
pop dx
ret
_join_lines:
dec di ; delete the char by pushing it into the gap
; Scroll the text below up (which clears the current line & markers for free)
mov al, 0 ; shift the text up to cover the current line
mov bl, 1 ; scroll one line
call scroll_text
dec dh ; Move the cursor onto the prev line (must be after scrolling)
; We might be bringing a line up from the bottom of the screen
call _repaint_bottom_line
.paint_joined_line:
; Need to skip scan_backward if we're at di == 0 because we can't check di-1
xor cx, cx ; if the check below passes, we have length 0
cmp di, [cs:user_code_start]
je .print_the_tail
; Setup the new current line
lea bp, [di-1]
call scan_backward
cmp cx, ROW_LENGTH
jae .shift_line_right
.print_the_tail:
; set the cursor column to one past the last char on the prev line
mov dl, START_COL
add dl, cl
; Find the length of the new tail
mov bp, si
call scan_forward
; Clip the printing to the end of the row
; bx = remaining space in the row from the current cursor position
mov bx, (ROW_LENGTH+START_COL)
sub bl, dl
cmp cx, bx
jb .no_clipping
mov cx, bx
mov ax, 0x0100 ; set to on ; right margin
call set_line_scroll_marker
.no_clipping:
mov bp, si
call print_line
jmp set_cursor_and_continue
.shift_line_right:
; Copy the last char in the row into the gap so we can print it
mov byte al, [es:si]
mov byte [es:di], al
; If we joined an empty line, we need to print a space
; Note: this is the garbage gap space so no need to restore the ending
cmp byte [es:di], `\n`
je .space
cmp byte [es:di], 0
je .space
jmp .no_space
.space:
mov byte [es:di], ' '
; If this happened we have nothing cut off, otherwise we just keep the
; existing marker (we know the line had some cut off on the right before this)
mov ax, 0x0000 ; set to off ; right margin
call set_line_scroll_marker
.no_space:
mov dl, START_COL ; print from the start column
mov bp, di
sub bp, ROW_LENGTH-1
mov cx, ROW_LENGTH
call print_line
mov dl, END_COL ; leave the cursor on the last char (first from the joined line)
jmp set_cursor_and_continue
; Moves the cursor left one nibble then continues typing_loop
; - Saves the byte in cl when moving to a new byte, also loads the existing
; data into cl if applicable
; - Calls move_up when moving to the next line
move_left:
; Don't do anything if we're already at the beginning of the buffer
cmp di, [cs:user_code_start]
je typing_loop
dec di
dec si
mov byte al, [es:di]
mov byte [es:si], al
cmp byte [es:si], `\n`
je _prev_line
cmp dl, START_COL
je _scroll_line_left
; Normal case, just go back one char
dec dl
jmp set_cursor_and_continue
_scroll_line_left:
; Just need to repaint the tail of the line and set scroll markers
mov bp, si
call scan_forward
cmp cx, ROW_LENGTH
jbe .no_clipping
; Set the right marker only when this is the first char we've cut off
cmp cx, ROW_LENGTH+1
jne .no_set_right_marker
mov ax, 0x0100 ; set to on ; right margin
call set_line_scroll_marker
.no_set_right_marker:
mov cx, ROW_LENGTH ; Print only up to our maximum
; fallthrough
.no_clipping:
mov bp, si
call print_line
; Check if we're at the start of the line and clear the left marker if so
cmp di, [cs:user_code_start]
je .at_beginning_of_line
cmp byte [es:di-1], `\n` ; -1 is safe because of the above check
je .at_beginning_of_line
jmp .keep_marker
.at_beginning_of_line:
mov ax, 0x0001 ; set to off ; left margin
call set_line_scroll_marker
.keep_marker:
jmp set_cursor_and_continue
_prev_line:
xor cx, cx ; line length is 0 if we're at di==0
cmp di, [cs:user_code_start] ; skip scanning if the first char is \n
je .move_up
lea bp, [di-1] ; scan from the char before the \n
call scan_backward
; fallthrough
; Scroll the screen (and set the final cursor row position)
.move_up:
cmp dh, START_ROW
jne .no_screen_scroll
; We have to make room for the previous line
mov al, 1
mov bl, 1
call scroll_text
jmp .paint_row
.no_screen_scroll:
; Just move the cursor up
dec dh
cmp cx, ROW_LENGTH-1
jbe .skip_repaint
; fallthrough
.paint_row:
; Clip the string to [:min(strlen, ROW_LENGTH-1)] chars at the end
; (-1 to leave a space for the user to type at the end)
mov dl, START_COL ; print from the beginning of the line
cmp cx, 0
je .skip_repaint
cmp cx, ROW_LENGTH-1
jbe .no_clipping
; Move the print pointer to the end of the line minus ROW_LENGTH-1
add bp, cx
mov cx, ROW_LENGTH ; num chars to print
sub bp, ROW_LENGTH-1 ; leave a space at the end
; Put a space in the buffer so we print it and overwrite on the screen the
; last char the user typed.
mov byte [es:di], ' '
; Set the scroll markers since we have some cut off to the left
mov ax, 0x0101 ; set to on ; left margin
call set_line_scroll_marker
mov ax, 0x0000 ; set to off ; right margin
call set_line_scroll_marker
call print_line
dec cx ; so we set the cursor after the last char not after the extra space
jmp .skip_repaint ; done
.no_clipping:
call print_line
; fallthrough
.skip_repaint:
; Set the cursor column and finally move it
mov dl, START_COL
add dl, cl
jmp set_cursor_and_continue
; Moves the cursor right one char (also wrapping lines) then continues typing_loop
move_right:
; Stop the cursor at the end of the user's code
cmp byte [es:si], 0
je typing_loop
mov byte al, [es:si]
mov byte [es:di], al
inc di
inc si ; we know we are not at USER_CODE_MAX because of the \0
; If we just hit the end for the first time, do a free gap reset
cmp byte [es:si], 0
jne .no_reset_gap ; We don't want to copy every time we move the cursor
call maybe_reset_gap ; This will do the check again and take the no-copy path
.no_reset_gap:
; Move the cursor to the next line once we're past the \n so we can append
; (remember, di is garbarge space and di-1 is the character before the cursor)
cmp byte [es:di-1], `\n`
je _next_line
cmp dl, END_COL
je _scroll_line_right
; Normal case, just go right one
inc dl
jmp set_cursor_and_continue
_scroll_line_right:
lea bp, [di-(ROW_LENGTH-1)] ; print starting from the second char in the row
mov cx, ROW_LENGTH ; number of chars to print
; Note: we know that bp >= 1 here because we scrolled right so we must have
; at least ROW_LENGTH characters in the buffer
; Print the left-side marker if this is the first time we've cut off a char
mov bx, [cs:user_code_start]
inc bx
cmp bp, bx ; if the first char in the line is the first char in the buffer
je .need_left_marker
; Now we know that bp >= 2 because bp != 1
cmp byte [es:bp-2], `\n` ; if the char before the first char in the line is \n
jne .already_have_left_marker
.need_left_marker:
mov ax, 0x0101 ; set to on ; left margin
call set_line_scroll_marker
.already_have_left_marker:
; Check if we're at the end of the line and clear the right marker if so