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