Rev 743 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5098 | clevermous | 1 | include "MACROS.INC" |
718 | jacekm | 2 | ; |
3 | ; OS function implementation |
||
4 | ; SmallC for KolibriOS |
||
5 | ; |
||
6 | |||
7 | ;B+ General definitions |
||
8 | |||
9 | ;B+ File defs |
||
10 | ;const |
||
11 | ;param |
||
12 | BAD equ -1 |
||
13 | files equ 100 |
||
743 | jacekm | 14 | save_buffer equ 0x20000 ;32 |
718 | jacekm | 15 | save_buffer_w equ 0x400000 ;32 |
16 | save_file_name equ 0x20000 |
||
17 | |||
18 | ;system |
||
19 | EOF equ -1 |
||
20 | |||
21 | ;memory |
||
22 | fileinfo equ I_END |
||
23 | start_data equ (fileinfo+16384) |
||
24 | ; |
||
25 | mem_heap equ 0x100000 |
||
26 | ; |
||
27 | g_handle equ 0x300000 |
||
28 | ;dword - pointer - relative to file |
||
29 | ;dword - begin of file |
||
30 | ;dword - file size |
||
31 | ;dword - 0/1 <=> read/write |
||
32 | ;E:. |
||
33 | |||
34 | ;E:. |
||
35 | |||
36 | init_osfunc: |
||
37 | ;B+ Init OS functions |
||
38 | ;B+ Clear file handles |
||
39 | mov edi,g_handle |
||
40 | mov ecx,files |
||
41 | shl ecx,2 ;*4 |
||
42 | xor eax,eax |
||
43 | cld |
||
44 | rep stosd |
||
45 | ;E:. |
||
46 | ret |
||
47 | ;E:. |
||
48 | |||
49 | ;B+ Main OS functions |
||
50 | ppp dd 70 |
||
51 | |||
52 | _OS_fopen: |
||
53 | ;B+ Implement "fopen" |
||
54 | ;esp+4 - mode |
||
55 | ;esp+8 - file name |
||
56 | |||
57 | ; mov eax,-1 |
||
58 | ; int 0x40 |
||
59 | |||
60 | ; mov ebx,[esp+8]; |
||
61 | ; push dword 10 |
||
62 | ; push dword [ppp] |
||
63 | ; push ebx |
||
64 | ; push dword 12 |
||
65 | ; call _outstrg |
||
66 | ; add esp,4*4 |
||
67 | ; add [ppp],10 |
||
68 | |||
69 | ; cmp byte [ebx+8],0 |
||
70 | ; jne .l |
||
71 | ; mov byte [ebx+8],'?' |
||
72 | ;.l: |
||
73 | ; cmp [ppp],80 |
||
74 | ; je .l |
||
75 | |||
76 | mov ecx , [esp+4] ; file mode |
||
77 | mov [file_mode],ecx |
||
78 | ;B+ Copy file name |
||
743 | jacekm | 79 | |
80 | |||
718 | jacekm | 81 | mov esi,[esp+8] |
82 | mov edi,[p_filename] |
||
83 | mov ecx,12 |
||
84 | .next_copy: |
||
85 | lodsb |
||
86 | ;fill name (space) |
||
87 | or al,al |
||
88 | jz .fill_space |
||
89 | ;set upper case |
||
90 | cmp al,'a' |
||
91 | jb .good_char |
||
92 | cmp al,'z' |
||
93 | ja .good_char |
||
94 | add al,'A'-'a' |
||
95 | .good_char: |
||
96 | stosb |
||
97 | dec ecx |
||
98 | jnz .next_copy |
||
99 | .fill_space: |
||
100 | mov al,' ' |
||
101 | cld |
||
102 | rep stosb |
||
103 | |||
104 | mov eax,[file_mode] |
||
105 | cmp byte [eax],'w' |
||
106 | jne .no_wri |
||
107 | |||
108 | |||
109 | ;B+ Copy file name |
||
110 | mov esi,[esp+8] |
||
111 | mov edi,[w_file_name] |
||
112 | mov ecx,12 |
||
113 | .next_copy2: |
||
114 | lodsb |
||
115 | ;fill name (space) |
||
116 | or al,al |
||
117 | jz .fill_space2 |
||
118 | ;set upper case |
||
119 | cmp al,'a' |
||
120 | jb .good_char2 |
||
121 | cmp al,'z' |
||
122 | ja .good_char2 |
||
123 | add al,'A'-'a' |
||
124 | .good_char2: |
||
125 | stosb |
||
126 | dec ecx |
||
127 | jnz .next_copy2 |
||
128 | .fill_space2: |
||
743 | jacekm | 129 | ;mov al,' ' |
130 | ;cld |
||
718 | jacekm | 131 | rep stosb |
132 | |||
133 | .no_wri: |
||
134 | |||
135 | |||
136 | |||
137 | ;E:. |
||
138 | ;B+ Find file handle |
||
139 | mov eax,g_handle |
||
140 | .new_handle: |
||
141 | cmp dword [eax+4],0 |
||
142 | je .find_place |
||
143 | add eax,16 |
||
144 | cmp eax,g_handle+files*16-16 |
||
145 | jne .new_handle |
||
146 | xor eax,eax ; no free handle |
||
147 | ret |
||
148 | .find_place: |
||
149 | ; TMP: mov eax,[.ccc] |
||
150 | ; TMP: add [.ccc],16 |
||
151 | ;E:. |
||
152 | push eax |
||
153 | ;B+ Test open mode |
||
154 | mov eax,[esp+4+4] |
||
155 | cmp byte [eax],'r' |
||
156 | je .open_read |
||
157 | cmp byte [eax],'w' |
||
158 | je .open_write |
||
159 | ;bad mode |
||
160 | add esp,4 |
||
161 | mov eax,eax ; invalid open mode |
||
162 | ret |
||
163 | ;E:. |
||
164 | |||
165 | ; TMP:.ccc dd g_handle |
||
166 | |||
167 | .open_read: |
||
168 | ;B+ Open for read |
||
169 | ;B+ Read file |
||
170 | |||
171 | ;Wait to read correct |
||
172 | mov ebx,100 |
||
173 | mov eax,5 |
||
174 | int 0x40 |
||
175 | |||
176 | mov eax,[g_fileend] |
||
177 | mov dword [file_parameters+2*4],2000 ;read all |
||
178 | mov dword [file_parameters+3*4],eax |
||
179 | |||
180 | mov dword [file_parameters],0 |
||
181 | mov ebx,file_parameters |
||
182 | mov eax,58 |
||
183 | int 0x40 |
||
184 | ;E:. |
||
185 | |||
186 | ;B+ TEST FILE FOUND |
||
187 | or eax,eax |
||
188 | jz .file_found |
||
189 | cmp eax,5 |
||
190 | je .file_found |
||
191 | |||
192 | ; mov ecx,eax ; eax |
||
193 | ; mov ebx,8 shl 16 + 0x0100 |
||
194 | ; mov edx,100 shl 16 + 120 |
||
195 | ; mov esi,0xffffff |
||
196 | ; mov eax,47 |
||
197 | ; int 0x40 |
||
198 | |||
199 | ;file not found - return 0 |
||
200 | add esp,4 |
||
201 | xor eax,eax |
||
202 | ret |
||
203 | .file_found: |
||
204 | ;E:. |
||
205 | pop eax |
||
206 | push ebx |
||
207 | xchg eax,ebx |
||
208 | ;B+ Fill file handle |
||
209 | ;save current pointer |
||
210 | xor eax,eax |
||
211 | mov [ebx],eax |
||
212 | |||
213 | ;save file begin |
||
214 | mov eax,[g_fileend] |
||
215 | mov [ebx+4],eax |
||
216 | |||
217 | ;save file size |
||
218 | pop eax |
||
219 | mov [ebx+8],eax |
||
220 | ;reserve file zone |
||
221 | add eax,7 |
||
222 | and eax,not 7 |
||
223 | add [g_fileend],eax |
||
224 | |||
225 | ;save file mode |
||
226 | mov eax,0 ;read |
||
227 | mov [ebx+12],eax |
||
228 | ;E:. |
||
229 | xchg eax,ebx ;return pointer place |
||
230 | ret |
||
231 | ;E:. |
||
232 | |||
233 | .open_write: |
||
234 | ;B+ Open for write |
||
235 | ;B+ Reserve filename |
||
236 | |||
237 | ; p_filename -> w_file_name |
||
238 | |||
239 | ;pusha |
||
240 | ; mov eax, w_file_name |
||
241 | ; mov ebx, [p_filename] |
||
242 | ;.ncpy: |
||
243 | ; mov ch, byte [ebx] |
||
244 | ; cmp ch, 0 |
||
245 | ; je .ecpy |
||
246 | ; mov [eax], ch |
||
247 | ; inc dword [eax] |
||
248 | ; inc dword [ebx] |
||
249 | ;jmp .ncpy |
||
250 | ; |
||
251 | ;.ecpy: |
||
252 | ; |
||
253 | ;popa |
||
254 | |||
255 | mov [save_buffer_p], save_buffer_w |
||
256 | |||
257 | mov esi,[p_filename] |
||
258 | mov edi,[g_fileend] |
||
259 | mov ecx,12 |
||
260 | cld |
||
261 | rep movsb |
||
262 | add [g_fileend],16 |
||
263 | ;E:. |
||
264 | pop ebx |
||
265 | ;B+ Fill file handle |
||
266 | ;save begin pointer |
||
267 | xor eax,eax |
||
268 | mov [ebx],eax |
||
269 | |||
270 | ;save file begin |
||
271 | mov eax,[g_fileend] |
||
272 | mov [ebx+4],eax |
||
273 | |||
274 | ;save file zone |
||
275 | mov dword [ebx+8],save_buffer |
||
276 | ;reserve file zone |
||
277 | add [g_fileend],save_buffer |
||
278 | |||
279 | ;save file mode |
||
280 | mov eax,1 ;write |
||
281 | mov [ebx+12],eax |
||
282 | ;E:. |
||
283 | xchg eax,ebx ;return pointer place |
||
284 | ret |
||
285 | ;E:. |
||
286 | |||
287 | ;E:. |
||
288 | |||
289 | _OS_fclos: |
||
290 | ;B+ Close file |
||
291 | ;esp+4 - file handle |
||
292 | |||
293 | ;B+ Test write mode - save file |
||
294 | mov eax,[esp+4] |
||
295 | mov eax,[eax+12] |
||
296 | cmp eax,1 |
||
297 | ;E:. |
||
298 | jne .no_write |
||
299 | |||
300 | mov eax, [esp+4] |
||
301 | mov ecx, [eax] |
||
302 | mov ebx, [eax+8] |
||
303 | |||
304 | mov ebx, [save_buffer_p] |
||
305 | sub ebx, save_buffer_w |
||
306 | ; ebx = number of read bytes = file size |
||
307 | ; save loaded file |
||
308 | mov [dest_info.bytes],ebx ; file size in bytes |
||
743 | jacekm | 309 | |
718 | jacekm | 310 | mov [dest_info.bytes+4], save_buffer_w |
311 | ;mov eax, [p_filename];[w_file_name] |
||
312 | ;mov [destination],eax |
||
313 | mov eax,70 |
||
314 | mov ebx,dest_info |
||
315 | mcall |
||
316 | |||
317 | ; check if 58 function failed |
||
318 | test eax,eax |
||
319 | je .ok_write |
||
320 | add eax,7 ; error number += 7 |
||
321 | cmp eax,6+7 |
||
322 | jna .copy_error |
||
323 | mov eax,7+7 |
||
324 | jmp .copy_error |
||
325 | |||
326 | .copy_error: |
||
327 | .ok_write: |
||
328 | |||
329 | |||
330 | ;E:. |
||
331 | jmp .read |
||
332 | |||
333 | .no_write: |
||
334 | ;B+ Test read mode - if no error end |
||
335 | cmp eax,0 |
||
336 | je .read |
||
337 | mov eax,BAD |
||
338 | ret |
||
339 | ;E:. |
||
340 | .read: |
||
341 | |||
342 | ;B+ Relace memory |
||
343 | ;find file size |
||
344 | mov eax,[esp+4] |
||
345 | mov ecx,[eax+8] |
||
346 | add ecx,7 |
||
347 | and ecx,not 7 |
||
348 | push ecx |
||
349 | |||
350 | ;mov memory |
||
351 | mov esi,[eax+4] |
||
352 | mov edi,esi |
||
353 | add esi,ecx |
||
354 | mov ecx,[g_fileend] |
||
355 | sub ecx,edi |
||
356 | jz .is_last |
||
357 | shr ecx,2 |
||
358 | inc ecx ;not neccessery |
||
359 | cld |
||
360 | rep movsd |
||
361 | ;update gl. memory |
||
362 | .is_last: |
||
363 | pop ecx |
||
364 | sub dword [g_fileend],ecx |
||
365 | |||
366 | ;update file pointers |
||
367 | mov edx,ecx |
||
368 | mov ecx,[eax+4] |
||
369 | mov eax,g_handle |
||
370 | .new_handle1: |
||
371 | mov ebx,[eax+4] |
||
372 | cmp ebx,ecx |
||
373 | jbe .no_update |
||
374 | sub ebx,edx |
||
375 | mov [eax+4],ebx |
||
376 | .no_update: |
||
377 | add eax,16 |
||
378 | cmp eax,g_handle+files*16 |
||
379 | jne .new_handle1 |
||
380 | |||
381 | ;clear handle |
||
382 | mov edi,[esp+4] |
||
383 | xor eax,eax |
||
384 | cld |
||
385 | stosd |
||
386 | stosd |
||
387 | stosd |
||
388 | stosd |
||
389 | ;E:. |
||
390 | ret |
||
391 | ;E:. |
||
392 | |||
393 | _OS_fgetc: |
||
394 | ;B+ Load char from file |
||
395 | ;esp+4 - input file |
||
396 | |||
397 | mov eax,[esp+4] |
||
398 | mov ebx,[eax] |
||
399 | cmp ebx,[eax+8] |
||
400 | je .eof |
||
401 | inc dword [eax] |
||
402 | add ebx,[eax+4] |
||
403 | movzx eax,byte [ebx] |
||
404 | ret |
||
405 | .eof: |
||
406 | mov eax,EOF |
||
407 | ret |
||
408 | ;E:. |
||
409 | |||
410 | ;rrr db 'g',0 |
||
411 | |||
412 | _OS_fputc: |
||
413 | ;B+ Save char to file |
||
414 | ;esp+4 - output file |
||
415 | ;esp+8 - char to write |
||
416 | |||
417 | ;push dword '<' |
||
418 | ;mov cl,1 |
||
419 | ;push dword 0 |
||
420 | ;call test_outch |
||
421 | ;add esp,8 |
||
422 | |||
423 | |||
424 | |||
425 | ;B+ Temp - write direct. |
||
426 | cmp dword [esp+4],__iob |
||
427 | jne .real_write0 |
||
428 | jmp _OS_exit |
||
429 | .real_write0: |
||
430 | cmp dword [esp+4],__iob+32 |
||
431 | jne .real_write1 |
||
432 | mov [print_textcolor],0x00ffff |
||
433 | jmp test_outch |
||
434 | .real_write1: |
||
435 | cmp dword [esp+4],__iob+64 |
||
436 | jne .real_write2 |
||
437 | mov [print_textcolor],0x77ffff |
||
438 | jmp test_outch |
||
439 | .real_write2: |
||
440 | ;E:. |
||
441 | |||
442 | mov ebx,[save_buffer_p] |
||
443 | mov eax,[esp+8] |
||
444 | mov [ebx],eax |
||
445 | inc dword [save_buffer_p] |
||
446 | |||
447 | |||
448 | ret |
||
449 | |||
450 | ;push dword '<' |
||
451 | ;mov cl,1 |
||
452 | ;push dword 0 |
||
453 | ;call test_outch |
||
454 | ;add esp,8 |
||
455 | |||
456 | mov eax,[esp+4] |
||
457 | mov ebx,[eax] |
||
458 | push ebx |
||
459 | cmp ebx,[eax+8] |
||
460 | jne .write_normal |
||
461 | |||
462 | |||
463 | |||
464 | ;B+ Alloc save_buffer bytes |
||
465 | ;mov memory |
||
466 | mov ebx,[esp+4+4] |
||
467 | mov esi,[g_fileend] |
||
468 | mov edi,esi |
||
469 | add edi,save_buffer-4 |
||
470 | mov ecx,esi |
||
471 | sub ecx,[ebx+4] |
||
472 | sub ecx,[ebx+8] |
||
473 | shr ecx,2 |
||
474 | jz .is_last |
||
475 | sub esi,4 |
||
476 | std |
||
477 | rep movsd |
||
478 | .is_last: |
||
479 | |||
480 | ;expand file size |
||
481 | add dword [eax+8],save_buffer |
||
482 | |||
483 | ;update file pointers |
||
484 | mov ebx,g_handle |
||
485 | .new_handle: |
||
486 | mov ecx,[ebx+4] |
||
487 | cmp [eax+4],ecx |
||
488 | jae .no_update |
||
489 | add dword [ebx+4],save_buffer |
||
490 | .no_update: |
||
491 | add ebx,16 |
||
492 | cmp ebx,g_handle+files*16-16 |
||
493 | jne .new_handle |
||
494 | ;E:. |
||
495 | |||
496 | .write_normal: |
||
497 | pop ebx |
||
498 | inc dword [eax] |
||
499 | add ebx,[eax+4] |
||
500 | mov cl,[esp+8] |
||
501 | mov byte [ebx],cl |
||
502 | |||
503 | ;sub [test_outch.x_coord],2 |
||
504 | ; |
||
505 | ;push dword '>' |
||
506 | ;mov cl,1 |
||
507 | ;push dword 0 |
||
508 | ;call test_outch |
||
509 | ;add esp,8 |
||
510 | ; |
||
511 | ;sub [test_outch.x_coord],6 |
||
512 | |||
513 | xor eax,eax |
||
514 | ret |
||
515 | ;E:. |
||
516 | |||
517 | _OS_callo: |
||
518 | ;B+ Alloc memory |
||
519 | ;find all size |
||
520 | mov eax,[esp+4] |
||
521 | mov ebx,[esp+8] |
||
522 | mul ebx |
||
523 | push eax |
||
524 | |||
525 | ;clear memory |
||
526 | mov edi,[.mem_p] |
||
527 | xor eax,eax |
||
528 | mov ecx,[esp] |
||
529 | cld |
||
530 | rep stosb |
||
531 | |||
532 | ;update new memory pointer |
||
533 | pop ebx |
||
534 | push dword [.mem_p] |
||
535 | add ebx,7 |
||
536 | and ebx,not 7 |
||
537 | add [.mem_p],ebx |
||
538 | |||
539 | ;return value |
||
540 | pop eax |
||
541 | ret |
||
542 | |||
543 | .mem_p dd mem_heap |
||
544 | ;E:. |
||
545 | |||
546 | _OS_exit: |
||
547 | ;B+ Exit program |
||
548 | ; ;TMP |
||
549 | ; mov eax,-1 |
||
550 | ; int 0x40 |
||
551 | mov esp,[exit_esp] |
||
552 | sub esp,4 |
||
553 | ret |
||
554 | ;E:. |
||
555 | |||
556 | ;E:. |
||
557 | |||
558 | |||
559 | |||
560 | |||
561 | ;B+ Test procedures |
||
562 | |||
563 | ;B+ Definitions |
||
564 | LEFTMARGIN equ 11 |
||
565 | BEGIN_CHARS equ 20 |
||
566 | NL equ 10 |
||
567 | ;E:. |
||
568 | |||
569 | print_textcolor dd 0x00ffff |
||
570 | |||
571 | _file_beg: |
||
572 | ;B+ Show begin of file - test fopen |
||
573 | ;esp+4 - file handle (descriptor) |
||
574 | |||
575 | mov eax,[esp+4] |
||
576 | mov ebx,10 shl 16 + 30 |
||
577 | mov ecx,[print_textcolor] |
||
578 | mov edx,[eax+4] |
||
579 | mov esi,BEGIN_CHARS |
||
580 | mov eax,4 |
||
581 | int 0x40 |
||
582 | ret |
||
583 | ;E:. |
||
584 | |||
585 | _outstrg: |
||
586 | ;B+ Draw black text - test function call |
||
587 | ;esp+4*4 - x |
||
588 | ;esp+4*3 - y |
||
589 | ;esp+4*2 - *c |
||
590 | ;esp+4*1 - len |
||
591 | |||
592 | mov ebx,[esp+4*4] |
||
593 | shl ebx,16 |
||
594 | mov bx,[esp+4*3] |
||
595 | mov ecx,[print_textcolor] |
||
596 | mov edx,[esp+4*2] |
||
597 | mov esi,[esp+4] |
||
598 | mov eax,4 |
||
599 | int 0x40 |
||
600 | ret |
||
601 | ;E:. |
||
602 | |||
603 | test_outch: |
||
604 | ;B+ Draw one char - use as _OS_fputc, to test printf(...) |
||
605 | ;esp+8 - char to write |
||
606 | |||
607 | ;this is test! \b \r - not nesessary |
||
608 | |||
609 | mov al,[esp+8] |
||
610 | cmp al,NL |
||
611 | jne .no_newline |
||
612 | add [.y_coord],10 |
||
613 | mov [.x_coord],LEFTMARGIN |
||
614 | ret |
||
615 | .no_newline: |
||
616 | |||
617 | mov ebx,[.x_coord] |
||
618 | shl ebx,16 |
||
619 | mov bx,word [.y_coord] |
||
620 | mov ecx,[print_textcolor] |
||
621 | mov [.out_char],al |
||
622 | mov edx,.out_char |
||
623 | mov esi,1 |
||
624 | mov eax,4 |
||
625 | int 0x40 |
||
626 | |||
627 | add [.x_coord],6 |
||
628 | ;mov eax,5 |
||
629 | ;mov ebx,5 |
||
630 | ;int 0x40 |
||
631 | ret |
||
632 | |||
633 | .x_coord dd LEFTMARGIN |
||
634 | .y_coord dd 60 |
||
635 | .out_char db 0 |
||
636 | ;E:. |
||
637 | |||
638 | ;E:. |
||
639 | |||
640 | ;B+ Data section |
||
641 | ;B+ Memory managment |
||
642 | g_fileend dd g_handle+files*4*4 ;from 2MB+100*4*4 |
||
643 | ;w_buff dd |
||
644 | ;E:. |
||
645 | |||
646 | save_buffer_p dd save_buffer_w |
||
647 | ;B+ File parameters |
||
648 | file_parameters: |
||
649 | dd 0x0 ; mode |
||
650 | dd 0x0 ; first block |
||
651 | dd 1000 ; block read |
||
652 | dd -1 ; return place |
||
653 | dd fileinfo ; work area |
||
654 | filepath: times 100 db 0 |
||
655 | |||
656 | file_mode dd 0 ;file mode |
||
657 | |||
658 | dest_info: ; DESTINATION FILEINFO |
||
659 | dd 2 |
||
660 | dd 0 |
||
661 | dd 0 |
||
662 | .bytes dd ? |
||
663 | dd save_buffer |
||
743 | jacekm | 664 | db 0 |
718 | jacekm | 665 | destination: |
666 | dd save_file_name |
||
743 | jacekm | 667 | ;db "EXAMPLE.ASM",0 |
718 | jacekm | 668 | |
669 | |||
743 | jacekm | 670 | |
718 | jacekm | 671 | w_file_name dd save_file_name |
672 | |||
673 | p_filename dd 0x0 |
||
674 | ;E:. |
||
675 | ;E:. |
||
676 | |||
677 | ;TO DO |
||
678 | mov eax,-1 |
||
679 | int 0x40 |
||
680 | ;TO DO' |
||
681 |