summaryrefslogtreecommitdiff
path: root/builtin_draw.asm
blob: d7aba3ac47bc9e224458d58834a60200923377dc (plain) (blame)
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
%ifndef _BUILTIN_DRAW_ASM
%define _BUILTIN_DRAW_ASM

%include "constants.asm"

jmp builtin_draw

align 1
builtin_draw_storage_active: db 00h
builtin_draw_exit: db 00h

align 2
builtin_draw_kbd_data: dw 0000h
builtin_draw_cursor_pos: dw 0000h


builtin_draw:
	push bp
	mov bp, sp
	pusha

	mov bx, 0
	mov dx, 07h

	.get_input:
		call builtin_draw_input
		call builtin_draw_perform
		;call builtin_draw_cursor_bounds_check
		cmp byte [builtin_draw_exit], 0
		jnz .return
		jmp .get_input
	.return:
		mov byte [builtin_draw_exit], 0		  ; reset exit status
		;mov byte [builtin_draw_storage_active], 0 ; reset storage state
		popa
		mov sp, bp
		pop bp
		ret


builtin_draw_input:
	mov ah, 01h
	int 16h
	jz .end

	mov [builtin_draw_kbd_data], ax

	mov ah, 00h
	int 16h

	.end:
		ret

builtin_draw_cursor_setup:
	mov ah, 02
	mov bh, 0
	mov dx, [builtin_draw_cursor_pos]
	ret


builtin_draw_toolbar:
	push es
	push ax
	mov ax, 0b800h			; set ES to video ram
	mov es, ax
	mov [es:0xf9e], byte 'C'	; direct write
	mov [es:0xf9f], dl		; direct write current COLOR
	pop ax
	pop es
	ret
	.msg_color: db 'Color:',0


builtin_draw_perform:
	push bp
	mov bp, sp

	call builtin_draw_toolbar
	jz .end

	mov ax, [builtin_draw_kbd_data]

	cmp al, 1bh		; ESC
	je .kill_program

	cmp ah, 3fh		; F5
	je .store_page

	cmp ah, 43h		; F9
	je .restore_page

	cmp ah, 4bh		; Left arrow
	je .cursor_left

	cmp ah, 4dh		; Right arrow
	je .cursor_right

	cmp ah, 50h		; Down arrow
	je .cursor_down

	cmp ah, 48h		; Up arrow
	je .cursor_up

	cmp al, '-'		; Minus
	je .color_down

	cmp al, '+'		; Plus
	je .color_up

	cmp al, 'f'		; f
	je .color_fill

	jmp .output

	.kill_program:
		mov byte [builtin_draw_exit], 1
		jmp .end

	.store_page:
		push ds
		push es

		mov ax, VIDEO_RAM 			; source
		mov ds, ax
		mov si, 0000h				; color text memory

		;mov ax, word [builtin_draw_storage_segment]	; destination segment
		mov ax, .STORAGE_SEGMENT
		mov es, ax
		mov di, 0000h				; page storage

		mov cx, 80 * 25

		cld
		rep movsw
		;mov byte [builtin_draw_storage_active], 1	; We have stored a page

		pop es
		pop ds
		jmp .end

	.restore_page:
		;cmp byte [builtin_draw_storage_active], 1	; Is there a page to restore?
		;jne .end

		push ds
		push es

		;mov ax, word [builtin_draw_storage_segment]	; source segment
		mov ax, .STORAGE_SEGMENT
		mov ds, ax
		mov si, 0000h				; page storage

		mov ax, VIDEO_RAM			; destination segment
		mov es, ax
		mov di, 0000h				; color text memory

		mov cx, 80 * 25

		cld
		rep movsw

		pop es
		pop ds
		jmp .end

	.cursor_up:
		push ax
		push bx
		push dx

		call builtin_draw_cursor_setup
		cmp dh, 0
		je .cursor_done

		dec dh
		int 10h
		mov [builtin_draw_cursor_pos], dx

		jmp .cursor_done

	.cursor_down:
		push ax
		push bx
		push dx

		call builtin_draw_cursor_setup

		cmp dh, 22
		jg .cursor_done

		inc dh
		int 10h
		mov [builtin_draw_cursor_pos], dx
		jmp .cursor_done

	.cursor_left:
		push ax
		push bx
		push dx

		call builtin_draw_cursor_setup
		cmp dl, 0
		je .cursor_done

		dec dl
		int 10h
		mov [builtin_draw_cursor_pos], dx
		jmp .cursor_done

	.cursor_right:
		push ax
		push bx
		push dx

		call builtin_draw_cursor_setup
		cmp dl, 78
		jg .cursor_done

		inc dl
		int 10h
		mov [builtin_draw_cursor_pos], dx
		jmp .cursor_done

	.cursor_done:
		pop dx
		pop bx
		pop ax
		jmp .end

	.color_down:
		sub dl, 0x10
		jmp .end

	.color_up:
		add dl, 0x10
		jmp .end

	.color_fill:
		push ax
		push bx
		push cx
		push dx

		mov ah, 06h
		mov al, 24
		mov bh, dl
		mov ch, 0
		mov cl, 0
		mov dh, 23
		mov dl, 79
		int 10h

		pop dx
		pop cx
		pop bx
		pop ax
		jmp .output

	.output:
		mov ah, 09h
		mov al, 20h
		mov bh, 0
		mov bl, dl
		mov cx, 1
		int 10h

		mov word [builtin_draw_kbd_data], 0000h

	.end:
		mov sp, bp
		pop bp
		ret

	.STORAGE_SEGMENT equ 7000h

%endif