/
image.asm
319 lines (290 loc) · 7.63 KB
/
image.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
;===================================================================================================
; Images
;
; Written By: Oded Cnaan (oded.8bit@gmail.com)
; Site: http://odedc.net
; Licence: GPLv3 (see LICENSE file)
; Package: GrLib
;
; Description:
; Managing images (not BMP)
;===================================================================================================
LOCALS @@
;======================================= SCREEN READ / WRITE================================================
;------------------------------------------------------------------------
; Copy a posrtion of the screen (x,y) into ScreenCache
;
; Input:
; memAddress = memory variable offset that stores the data. Should be in the correct length (w*h)
; memAddressSeg - segment of memAddress
; x = top left X coordinate (must be 0..320-width)
; y = top left Y coordinate (must be 0..200-height)
; w = width of the area to copy
; h = height of the area to copy
;
; SaveScreen(word *memAddress, word x, word y, word w, word h)
;------------------------------------------------------------------------
PROC SaveScreen
store_sp_bp
sub sp,2
pusha
push es
push ds
; now the stack is
; bp-2 => current y
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => h
; bp+6 => w
; bp+8 => y
; bp+10 => x
; bp+12 => memSeg
; bp+14 => *memAddress
; saved registers
;{
rectHeight equ [word bp+4]
rectWidth equ [word bp+6]
yCoord equ [word bp+8]
xCoord equ [word bp+10]
memSeg equ [word bp+12]
memAddress equ [word bp+14]
vatCurY equ [word bp-2]
;}
is_valid_coord_vga_mem bp+10,bp+8,bp+6,bp+4
cmp ax,1
je @@end ; invalid coords
; set ES
push [WORD _dss]
pop es
; set DS
push [WORD GR_START_ADDR]
pop ds
mov ax, rectHeight
add ax, yCoord
mov bx, yCoord
mov vatCurY, bx
mov dx,0 ; dx = row in buffer
@@cpy:
push ax
push dx
; set SI
translate_coord_to_vga_addr bp+10,bp-2
mov si, di
; set DI
mov di, memAddress
mov ax, dx
mov dx, rectHeight
mul dx ; y * height
add di, ax
push memSeg
pop es
; set length
mov cx, rectWidth
; copy DS:SI to ES:DI
cld
rep movsb
inc vatCurY
pop dx
inc dx
pop ax
cmp vatCurY,ax
jbe @@cpy
@@end:
pop ds
pop es
popa
restore_sp_bp
ret 12
ENDP SaveScreen
;------------------------------------------------------------------------
; Copy ScreenCache into the screen memory (x,y)
;
; Input:
; memAddress = memory variable offset that stores the data. Should be in the right length (w*h)
; memAddressSeg - segment of memAddress
; x = top left X coordinate (must be 0..320-width)
; y = top left Y coordinate (must be 0..200-height)
; w = width of the area to copy
; h = height of the area to copy
;
; WriteScreen(word *memAddress, word x, word y, word w, word h)
;------------------------------------------------------------------------
PROC WriteScreen
store_sp_bp
sub sp, 2
pusha
push es
push ds
; now the stack is
; bp-2 => current y
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => h
; bp+6 => w
; bp+8 => y
; bp+10 => x
; bp+12 => memSeg
; bp+14 => *memAddress
; saved registers
;{
rectHeight equ [word bp+4]
rectWidth equ [word bp+6]
yCoord equ [word bp+8]
xCoord equ [word bp+10]
memSeg equ [word bp+12]
memAddress equ [word bp+14]
vatCurY equ [word bp-2]
;}
is_valid_coord_vga_mem bp+10,bp+8,bp+6,bp+4
cmp ax,1
je @@end ; invalid coords
; set DS
push [WORD _dss]
pop ds
; set ES
push [WORD GR_START_ADDR]
pop es
mov ax, rectHeight
add ax, yCoord ; ax = yBottom
mov bx, yCoord ; bx = current y on screen
mov vatCurY, bx
mov dx, 0 ; dx = current y in buffer
@@cpy:
push ax
push dx
; set DI
translate_coord_to_vga_addr bp+10,bp-2
; set SI
mov si, memAddress
mov ax, dx
mov dx, rectHeight
mul dx ; y * height
add si, ax
push memSeg
pop ds
; set length
mov cx, rectWidth
; copy DS:SI to ES:DI
cld
rep movsb
inc vatCurY
pop dx
inc dx
pop ax
cmp vatCurY,ax
jbe @@cpy
@@end:
pop ds
pop es
popa
restore_sp_bp
ret 12
ENDP WriteScreen
;------------------------------------------------------------------------
; Writes BLACK to a (x,y) coordinates
;
; Input:
; x = top left X coordinate (must be 0..320-width)
; y = top left Y coordinate (must be 0..200-height)
; w = width of the area to copy
; h = height of the area to copy
;
; WriteBlackScreen(word x, word y, word w, word h)
;------------------------------------------------------------------------
PROC WriteBlackScreen
store_sp_bp
sub sp, 2
pusha
push es
push ds
; now the stack is
; bp-2 => current y
; bp+0 => old base pointer
; bp+2 => return address
; bp+4 => h
; bp+6 => w
; bp+8 => y
; bp+10 => x
; saved registers
;{
rectHeight equ [word bp+4]
rectWidth equ [word bp+6]
yCoord equ [word bp+8]
xCoord equ [word bp+10]
vatCurY equ [word bp-2]
;}
is_valid_coord_vga_mem bp+10,bp+8,bp+6,bp+4
cmp ax,1
je @@end ; invalid coords
; set ES and DS
push [WORD GR_START_ADDR]
pop es
push [WORD _dss]
pop ds
mov dx, rectHeight ; h
add dx, yCoord ; dx = yBottom
mov bx, yCoord ; bx = current y on screen
mov vatCurY, bx
xor ax,ax
mov al, GR_COLOR_BLACK
@@cpy:
; set DI
translate_coord_to_vga_addr bp+10,bp-2
; set length
mov cx, rectWidth
; copy DS:SI to ES:DI
cld
rep stosb
inc vatCurY
cmp vatCurY,dx
jbe @@cpy
@@end:
pop ds
pop es
popa
restore_sp_bp
ret 8
ENDP WriteBlackScreen
;////////////////////////////////////////////////////////////////////////////
; FUNCTION LIKE MACROS
;////////////////////////////////////////////////////////////////////////////
;----------------------------------------------------------------------
; Copy a posrtion of the screen (x,y) into ScreenCache
;
; grm_SaveScreen (memAddress, memSeg, x, y, w, h)
;----------------------------------------------------------------------
MACRO grm_SaveScreen memAddress, memSeg, x, y, w, h
push memAddress
push memSeg
push x
push y
push w
push h
call SaveScreen
ENDM
;----------------------------------------------------------------------
; Copy ScreenCache into the screen memory (x,y)
;
; grm_WriteScreen (memAddress, memSeg, x, y, w, h)
;----------------------------------------------------------------------
MACRO grm_WriteScreen memAddress, memSeg, x, y, w, h
push memAddress
push memSeg
push x
push y
push w
push h
call WriteScreen
ENDM
;----------------------------------------------------------------------
; Copy ScreenCache into the screen memory (x,y)
;
; grm_WriteBlackScreen (x, y, w, h)
;----------------------------------------------------------------------
MACRO grm_WriteBlackScreen x, y, w, h
push x
push y
push w
push h
call WriteBlackScreen
ENDM