]> git.sur5r.net Git - cc65/blob - libsrc/lynx/open.s
f4d0471b000a3bebedc39ea9b5b9a43d8d52d9ff
[cc65] / libsrc / lynx / open.s
1 ;
2 ; Karri Kaksonen, 2010
3 ;
4 ; This function reads the directory entry for file "name".
5 ;
6 ; The name is actually plain ASCII string starting from
7 ; "0", "1", up to "4095" which is the largest file number we can handle.
8 ;
9 ; open() does not take part in what kind of cart we have. If it is RAM
10 ; you may also be able to write into it. Therefore we allow both reads
11 ; and writes in this routine.
12 ;
13 ; int open(const char *name, int flags, ...)
14 ;
15 ; As helper functions we also provide.
16 ; void openn(int fileno)
17 ;
18         .importzp       sreg, tmp3
19         .macpack        longbranch
20         .import         _atoi
21         .import         _read
22         .import         _lseek
23         .import         addysp,popax,pushax,decsp6,pusha0,pusheax,ldaxysp
24         .import         aslax3,axlong,tosaddeax,steaxysp,stax0sp,incsp8
25         .import         ldax0sp
26         .importzp       _FileEntry
27         .importzp       _FileStartBlock
28         .importzp       _FileCurrBlock
29         .importzp       _FileBlockOffset
30         .import         __STARTOFDIRECTORY__
31         .export         _open
32         .export         _openn
33
34         .include        "errno.inc"
35         .include        "fcntl.inc"
36
37 .segment        "DATA"
38
39 _startofdirectory:
40         .dword  __STARTOFDIRECTORY__
41
42 ; ---------------------------------------------------------------
43 ; int __near__ open (__near__ const unsigned char*, int)
44 ; ---------------------------------------------------------------
45
46 .segment        "CODE"
47
48 .proc   _open
49
50 .segment        "CODE"
51
52         dey
53         dey
54         dey
55         dey
56         beq     parmok
57         jsr     addysp
58
59 parmok: jsr     popax
60         sta     tmp3
61         and     #(O_RDWR | O_CREAT)
62         cmp     #O_RDONLY
63         beq     flagsok
64         cmp     #(O_WRONLY | O_CREAT)
65         beq     flagsok
66         jsr     popax
67         lda     #EINVAL
68         jmp     __directerrno
69
70 flagsok:
71         jsr     popax
72         jsr     _atoi
73         jsr     _openn
74         ldx     #$00
75         lda     #$01
76         stx     __oserror
77         rts
78
79 .endproc
80
81 ; ---------------------------------------------------------------
82 ; void __near__ __fastcall__ openn (int)
83 ; ---------------------------------------------------------------
84
85 .segment        "CODE"
86
87 .proc   _openn: near
88
89 .segment        "CODE"
90
91         jsr     pushax
92         jsr     decsp6
93         lda     #$01
94         jsr     pusha0
95         lda     _startofdirectory+3
96         sta     sreg+1
97         lda     _startofdirectory+2
98         sta     sreg
99         ldx     _startofdirectory+1
100         lda     _startofdirectory
101         jsr     pusheax
102         ldy     #$0D
103         jsr     ldaxysp
104         jsr     aslax3
105         jsr     axlong
106         jsr     tosaddeax
107         jsr     pusheax
108         ldx     #$00
109         txa
110         jsr     _lseek
111         ldy     #$02
112         jsr     steaxysp
113         lda     #$01
114         jsr     pusha0
115         lda     _FileEntry
116         ldx     _FileEntry+1
117         jsr     pushax
118         ldx     #$00
119         lda     #$08
120         jsr     _read
121         jsr     stax0sp
122         jmp     incsp8
123
124 .endproc
125