1 /*
2 * blockdev.c
3 *
4 * Written by
5 * Andreas Boose <viceteam@t-online.de>
6 *
7 * This file is part of VICE, the Versatile Commodore Emulator.
8 * See README for copyright notice.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 * 02111-1307 USA.
24 *
25 */
26
27 #include "vice.h"
28
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #include "blockdev.h"
35 #include "log.h"
36 #include "types.h"
37
38
39 /*static log_t blockdev_log = LOG_DEFAULT;*/
40
41 static int device;
42
43
blockdev_open(const char * name,unsigned int * read_only)44 int blockdev_open(const char *name, unsigned int *read_only)
45 {
46 if (*read_only == 0) {
47 device = open(name, O_RDWR);
48
49 if (device == -1) {
50 device = open(name, O_RDONLY);
51 if (device == -1)
52 return -1;
53 *read_only = 1;
54 }
55 } else {
56 device = open(name, O_RDONLY);
57
58 if (device == -1)
59 return -1;
60 }
61
62 return 0;
63 }
64
blockdev_close(void)65 int blockdev_close(void)
66 {
67 return close(device);
68 }
69
70 /*-----------------------------------------------------------------------*/
71
blockdev_read_sector(BYTE * buf,unsigned int track,unsigned int sector)72 int blockdev_read_sector(BYTE *buf, unsigned int track, unsigned int sector)
73 {
74 off_t offset;
75
76 offset = ((track - 1) * 40 + sector) * 256;
77
78 lseek(device, offset, SEEK_SET);
79
80 if (read(device, (void *)buf, 256) != 256)
81 return -1;
82
83 return 0;
84 }
85
blockdev_write_sector(const BYTE * buf,unsigned int track,unsigned int sector)86 int blockdev_write_sector(const BYTE *buf, unsigned int track, unsigned int sector)
87 {
88 off_t offset;
89
90 offset = ((track - 1) * 40 + sector) * 256;
91
92 lseek(device, offset, SEEK_SET);
93
94 if (write(device, (void *)buf, 256) != 256)
95 return -1;
96
97 return 0;
98 }
99
100 /*-----------------------------------------------------------------------*/
101
blockdev_init(void)102 void blockdev_init(void)
103 {
104 }
105
blockdev_resources_init()106 int blockdev_resources_init()
107 {
108 return 0;
109 }
110
blockdev_cmdline_options_init()111 int blockdev_cmdline_options_init()
112 {
113 return 0;
114 }
115
116