]> git.sur5r.net Git - cc65/blob - libsrc/lynx/open.s
Replace hard returns with an "else", add an error for non-IDENT tokens, and test...
[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         .import         lynxskip0, lynxblock
27         .importzp       _FileEntry
28         .importzp       _FileStartBlock
29         .importzp       _FileCurrBlock
30         .importzp       _FileBlockOffset
31         .import         __STARTOFDIRECTORY__
32         .export         _open
33         .export         _openn
34
35         .include        "errno.inc"
36         .include        "fcntl.inc"
37
38 .segment        "DATA"
39
40 _startofdirectory:
41         .dword  __STARTOFDIRECTORY__
42
43 ; ---------------------------------------------------------------
44 ; int __near__ open (__near__ const unsigned char*, int)
45 ; ---------------------------------------------------------------
46
47 .segment        "CODE"
48
49 .proc   _open
50
51 .segment        "CODE"
52
53         dey
54         dey
55         dey
56         dey
57         beq     parmok
58         jsr     addysp
59
60 parmok: jsr     popax
61         sta     tmp3
62         and     #(O_RDWR | O_CREAT)
63         cmp     #O_RDONLY
64         beq     flagsok
65         cmp     #(O_WRONLY | O_CREAT)
66         beq     flagsok
67         jsr     popax
68         lda     #EINVAL
69         jmp     __directerrno
70
71 flagsok:
72         jsr     popax
73         jsr     _atoi
74         jsr     _openn
75         ldx     #$00
76         lda     #$01
77         stx     __oserror
78         rts
79
80 .endproc
81
82 ; ---------------------------------------------------------------
83 ; void __near__ __fastcall__ openn (int)
84 ; ---------------------------------------------------------------
85
86 .segment        "CODE"
87
88 .proc   _openn: near
89
90 .segment        "CODE"
91
92         jsr     pushax
93         jsr     decsp6
94         lda     #$01
95         jsr     pusha0
96         lda     _startofdirectory+3
97         sta     sreg+1
98         lda     _startofdirectory+2
99         sta     sreg
100         ldx     _startofdirectory+1
101         lda     _startofdirectory
102         jsr     pusheax
103         ldy     #$0D
104         jsr     ldaxysp
105         jsr     aslax3
106         jsr     axlong
107         jsr     tosaddeax
108         jsr     pusheax
109         ldx     #$00
110         txa
111         jsr     _lseek
112         ldy     #$02
113         jsr     steaxysp
114         lda     #$01
115         jsr     pusha0
116         lda     #<_FileEntry
117         ldx     #>_FileEntry
118         jsr     pushax
119         ldx     #$00
120         lda     #$08
121         jsr     _read
122         lda     _FileStartBlock
123         sta     _FileCurrBlock
124         jsr     lynxblock
125         lda     _FileBlockOffset+1
126         eor     #$FF
127         tay
128         lda     _FileBlockOffset
129         eor     #$FF
130         tax
131         jsr     lynxskip0
132         jsr     stax0sp
133         jmp     incsp8
134
135 .endproc
136