blob: 69ade28fcc439f28367c40afc44afef9dd1f0d63 [file] [log] [blame]
adamdunkelsc8a32572003-08-09 13:15:58 +00001/*
2 * Copyright (c) 2003, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This file is part of the Contiki desktop environment
34 *
35 * $Id: c64-fs-raw.c,v 1.1 2003/08/09 13:15:58 adamdunkels Exp $
36 *
37 */
38
39#include "c64-fs-raw.h"
40
41#include <string.h>
42
43struct directory_entry {
44 unsigned char type;
45 unsigned char track, sect;
46 unsigned char name[16];
47 unsigned char reltrack, relsect, relreclen;
48 unsigned char unused1, unused2, unused3, unused4;
49 unsigned char tmptrack, tmpsect;
50 unsigned char blockslo, blockshi;
51};
52
53
54extern unsigned char _c64_fs_dirbuf[256];
55extern unsigned char _c64_fs_dirbuftrack, _c64_fs_dirbufsect;
56
57extern unsigned char _c64_fs_filebuf[256];
58extern unsigned char _c64_fs_filebuftrack, _c64_fs_filebufsect;
59
60void _c64_fs_readdirbuf(unsigned char track, unsigned char sect);
61
62
63/*-----------------------------------------------------------------------------------*/
64int __fastcall__
65c64_fs_read_raw(register struct c64_fs_file *f, char *buf, int len)
66{
67 int i;
68 unsigned char fptr, ftrack, fsect;
69
70 /* Check if current block is already in buffer, and if not read it
71 from disk. */
72 if(_c64_fs_filebuftrack != f->track ||
73 _c64_fs_filebufsect != f->sect) {
74 _c64_fs_filebuftrack = f->track;
75 _c64_fs_filebufsect = f->sect;
76 c64_dio_read_block(_c64_fs_filebuftrack,
77 _c64_fs_filebufsect, _c64_fs_filebuf);
78 }
79
80 if(_c64_fs_filebuf[0] == 0 &&
81 f->ptr == _c64_fs_filebuf[1]) {
82 return 0; /* EOF */
83 }
84
85 fptr = f->ptr;
86 ftrack = f->track;
87 fsect = f->sect;
88
89 for(i = 0; i < len; ++i) {
90 *buf = _c64_fs_filebuf[fptr];
91
92 ++fptr;
93 if(_c64_fs_filebuf[0] == 0) {
94 if(fptr == _c64_fs_filebuf[1]) {
95 /* End of file reached, we return the amount of bytes read so
96 far. */
97 return i + 1;
98 }
99 } else if(fptr == 0) {
100
101 /* Read new block into buffer and set buffer state
102 accordingly. */
103 _c64_fs_filebuftrack = ftrack = _c64_fs_filebuf[0];
104 _c64_fs_filebufsect = fsect = _c64_fs_filebuf[1];
105 fptr = 2;
106 c64_dio_read_block(_c64_fs_filebuftrack,
107 _c64_fs_filebufsect, _c64_fs_filebuf);
108 }
109
110 ++buf;
111 }
112 return i;
113}
114/*-----------------------------------------------------------------------------------*/
115int
116c64_fs_read_next(register struct c64_fs_file *f, int len)
117{
118 int i;
119
120 /* Check if current block is already in buffer, and if not read it
121 from disk. */
122 if(_c64_fs_filebuftrack != f->track ||
123 _c64_fs_filebufsect != f->sect) {
124 _c64_fs_filebuftrack = f->track;
125 _c64_fs_filebufsect = f->sect;
126 c64_dio_read_block(_c64_fs_filebuftrack,
127 _c64_fs_filebufsect, _c64_fs_filebuf);
128 }
129
130 if(_c64_fs_filebuf[0] == 0 &&
131 f->ptr == _c64_fs_filebuf[1]) {
132 return 0; /* EOF */
133 }
134
135 for(i = 0; i < len; ++i) {
136
137 ++f->ptr;
138 if(_c64_fs_filebuf[0] == 0) {
139 if(f->ptr == _c64_fs_filebuf[1]) {
140 /* End of file reached, we return the amount of bytes read so
141 far. */
142 return i + 1;
143 }
144 } else if(f->ptr == 0) {
145 /* Read new block into buffer and set buffer state
146 accordingly. */
147 _c64_fs_filebuftrack = f->track = _c64_fs_filebuf[0];
148 _c64_fs_filebufsect = f->sect = _c64_fs_filebuf[1];
149 f->ptr = 2;
150 c64_dio_read_block(_c64_fs_filebuftrack,
151 _c64_fs_filebufsect, _c64_fs_filebuf);
152 }
153 }
154 return i;
155}
156/*-----------------------------------------------------------------------------------*/
157void
158c64_fs_readdir_raw(register struct c64_fs_dir *d,
159 register struct c64_fs_dirent *f)
160{
161 struct directory_entry *de;
162 int i;
163 register char *nameptr;
164
165 _c64_fs_readdirbuf(d->track, d->sect);
166 de = (struct directory_entry *)&_c64_fs_dirbuf[d->ptr];
167 nameptr = de->name;
168 for(i = 0; i < 16; ++i) {
169 if(*nameptr == 0xa0) {
170 *nameptr = 0;
171 break;
172 }
173 ++nameptr;
174 }
175 strncpy(f->name, de->name, 16);
176 f->track = de->track;
177 f->sect = de->sect;
178 f->size = de->blockslo + (de->blockshi >> 8);
179}
180/*-----------------------------------------------------------------------------------*/
181unsigned char
182c64_fs_readdir_next(struct c64_fs_dir *d)
183{
184 struct directory_entry *de;
185 again:
186 _c64_fs_readdirbuf(d->track, d->sect);
187 if(d->ptr == 226) {
188 if(_c64_fs_dirbuf[0] == 0) {
189 return 1;
190 }
191 d->track = _c64_fs_dirbuf[0];
192 d->sect = _c64_fs_dirbuf[1];
193 d->ptr = 2;
194 } else {
195 d->ptr += 32;
196 }
197
198 de = (struct directory_entry *)&_c64_fs_dirbuf[d->ptr];
199 if(de->type == 0) {
200 goto again;
201 }
202 return 0;
203}
204/*-----------------------------------------------------------------------------------*/