]> git.sur5r.net Git - cc65/blob - libsrc/apple2/lseek.s
Adds test code for the Atari (xex) linker file format.
[cc65] / libsrc / apple2 / lseek.s
1 ;
2 ; Peter Ferrie, 21.11.2014
3 ;
4 ; off_t __fastcall__ lseek(int fd, off_t offset, int whence);
5 ;
6
7         .export         _lseek
8         .import         popax, popptr1
9         .macpack        cpu
10
11         .include        "zeropage.inc"
12         .include        "errno.inc"
13         .include        "mli.inc"
14         .include        "filedes.inc"
15
16 _lseek:
17         ; Save whence
18         sta     tmp1
19         stx     tmp2
20
21         ; Get and save offset
22         jsr     popptr1
23         jsr     popax
24         sta     ptr2
25         stx     ptr2+1
26
27         ; Get and process fd
28         jsr     popax
29         jsr     getfd           ; Returns A, Y and C
30         bcs     errno
31
32         ; Check for device
33         cmp     #$80
34         bcs     einval
35
36         ; Valid whence values are 0..2
37         ldx     tmp2
38         bne     einval
39         ldx     tmp1
40         cpx     #3
41         bcs     einval
42
43         ; Set fd
44         sta     mliparam + MLI::MARK::REF_NUM
45
46         txa
47         beq     cur
48         lda     #GET_EOF_CALL
49         dex
50         beq     end
51
52 ; SEEK_SET
53         dex
54         txa
55         tay
56         beq     seek_common
57
58 ; SEEK_CUR
59 cur:
60         lda     #GET_MARK_CALL
61
62 ; SEEK_END
63 end:
64         ; MARK_COUNT must == EOF_COUNT, otherwise unexpected behaviour
65         .assert MARK_COUNT = EOF_COUNT, error
66         ldx     #MARK_COUNT
67         jsr     callmli
68         bcs     oserr
69         lda     mliparam + MLI::MARK::POSITION
70         ldx     mliparam + MLI::MARK::POSITION+1
71         ldy     mliparam + MLI::MARK::POSITION+2
72
73 seek_common:
74         adc     ptr1
75         sta     mliparam + MLI::MARK::POSITION
76         txa
77         adc     ptr1+1
78         sta     mliparam + MLI::MARK::POSITION+1
79         tya
80         adc     ptr2
81         sta     mliparam + MLI::MARK::POSITION+2
82         lda     #$00
83         adc     ptr2+1
84         bne     einval          ; less than 0 or greater than 2^24 - 1
85
86         ; Set file pointer
87         lda     #SET_MARK_CALL
88         ldx     #MARK_COUNT
89         jsr     callmli
90         bcs     oserr
91
92         ; Need to return the position in EAX
93 .if (.cpu .bitand ::CPU_ISET_65SC02)
94         stz     sreg+1
95 .else
96         lda     #$00
97         sta     sreg+1
98 .endif
99         lda     mliparam + MLI::MARK::POSITION+2
100         sta     sreg
101         ldx     mliparam + MLI::MARK::POSITION+1
102         lda     mliparam + MLI::MARK::POSITION
103
104         rts
105
106         ; Load errno code
107 einval: lda     #EINVAL
108
109         ; Set __errno
110 errno:  jsr     __directerrno   ; leaves -1 in AX
111         stx     sreg            ; extend return value to 32 bits
112         stx     sreg+1
113         rts
114
115         ; Set __oserror
116 oserr:  jsr     __mappederrno   ; leaves -1 in AX
117         stx     sreg            ; extend return value to 32 bits
118         stx     sreg+1
119         rts