1 /*
2  *  KCemu -- The emulator for the KC85 homecomputer series and much more.
3  *  Copyright (C) 1997-2010 Torsten Paul
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include <unistd.h>
21 #include <sys/stat.h>
22 
23 #include "kc/system.h"
24 
25 #include "kc/floppy.h"
26 
27 #include "cmd/cmd.h"
28 
29 #include "libdbg/dbg.h"
30 
31 class CMD_floppy_attach : public CMD
32 {
33 private:
34   Floppy *_f;
35 public:
CMD_floppy_attach(Floppy * f,const char * cmdname)36   CMD_floppy_attach(Floppy *f, const char *cmdname) : CMD("floppy-attach")
37     {
38       _f = f;
39       register_cmd(cmdname);
40     }
41 
execute(CMD_Args * args,CMD_Context context)42   void execute(CMD_Args *args, CMD_Context context)
43     {
44       const char *filename;
45 
46       if (!args)
47         return;
48 
49       filename = args->get_string_arg("filename");
50       if (!filename)
51         return;
52 
53       _f->attach(filename);
54     }
55 };
56 
Floppy(const char * cmdname)57 Floppy::Floppy(const char *cmdname)
58 {
59   _disk_prop = NULL;
60 
61   CMD *cmd;
62   cmd = new CMD_floppy_attach(this, cmdname);
63 }
64 
~Floppy(void)65 Floppy::~Floppy(void)
66 {
67 }
68 
69 int
get_head_count(void)70 Floppy::get_head_count(void)
71 {
72   if (_disk_prop == NULL)
73     return -1;
74 
75   return _disk_prop->head_count;
76 }
77 
78 int
get_cylinder_count(void)79 Floppy::get_cylinder_count(void)
80 {
81   if (_disk_prop == NULL)
82     return -1;
83 
84   return _disk_prop->cylinder_count;
85 }
86 
87 int
get_sector_size(void)88 Floppy::get_sector_size(void)
89 {
90   if (_disk_prop == NULL)
91     return -1;
92 
93   return _disk_prop->sector_size;
94 }
95 
96 int
get_sectors_per_cylinder(void)97 Floppy::get_sectors_per_cylinder(void)
98 {
99   if (_disk_prop == NULL)
100     return -1;
101 
102   return _disk_prop->sectors_per_cylinder;
103 }
104 
105 bool
attach(const char * filename)106 Floppy::attach(const char *filename)
107 {
108   if (filename == NULL)
109     {
110       libdisk_close(&_disk_prop);
111       return true;
112     }
113 
114   if (libdisk_open(&_disk_prop, filename) < 0)
115     {
116       //cerr << "Can't attach disk-image '" << filename << "'!" << endl;
117       return false;
118     }
119   //cerr << "Attached disk-image '" << filename << "'" << endl;
120   return true;
121 }
122 
123 bool
seek(int head,int cylinder,int sector)124 Floppy::seek(int head, int cylinder, int sector)
125 {
126   if (_disk_prop == NULL)
127     return false;
128 
129   if (libdisk_seek(&_disk_prop, head, cylinder, sector) < 0)
130     return false;
131 
132   return true;
133 }
134 
135 int
read_sector(byte_t * buf,int len)136 Floppy::read_sector(byte_t *buf, int len)
137 {
138   return libdisk_read_sector(&_disk_prop, buf, len);
139 }
140 
141 int
write_sector(byte_t * buf,int len)142 Floppy::write_sector(byte_t *buf, int len)
143 {
144   return libdisk_write_sector(&_disk_prop, buf, len);
145 }
146