-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_fdd.c
211 lines (191 loc) · 5.02 KB
/
io_fdd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*-
* Copyright (c) 2005-2020 Poul-Henning Kamp
* All rights reserved.
*
* Author: Poul-Henning Kamp <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/endian.h>
#include "rc3600.h"
#define FDD_BPS 128
#define FDD_SPT 26
#define FDD_TPD 77
#define FDD_SIZE (FDD_TPD * FDD_SPT * FDD_BPS)
struct io_fdd {
int speed;
struct iodev *iop;
uint8_t img[FDD_SIZE];
uint8_t wbuf[FDD_BPS];
uint8_t sect;
uint8_t track;
unsigned r_ptr;
unsigned w_ptr;
};
static void
dev_fdd_iofunc(struct iodev *iop, uint16_t ioi, uint16_t *reg)
{
struct io_fdd *fp;
fp = iop->priv;
if (IO_OPER(ioi) == IO_DIB) {
*reg = fp->img[fp->r_ptr++];
return;
}
if (IO_OPER(ioi) == IO_DOB) {
fp->wbuf[fp->w_ptr] = *reg;
if (fp->w_ptr < FDD_BPS)
fp->w_ptr++;
return;
}
//iop->ireg_c = (fp->track << 8) | 0xfb;
iop->ireg_c = 0xfb;
std_io_ins(iop, ioi, reg);
if (IO_ACTION(ioi) == IO_START) {
switch (iop->oreg_a & 0x0300) {
case 0x0000:
fp->sect = iop->oreg_a & 0x00ff;
dev_trace(iop, "FDD READ track %u sector %u\n",
fp->track, fp->sect);
assert(fp->sect >= 1);
assert(fp->sect <= FDD_SPT);
fp->r_ptr = fp->sect - 1;
fp->r_ptr *= FDD_BPS;
fp->r_ptr += fp->track * FDD_SPT * FDD_BPS;
fp->w_ptr = 0;
break;
case 0x0100:
fp->sect = iop->oreg_a & 0x00ff;
dev_trace(iop, "FDD WRITE track %u sector %u\n",
fp->track, fp->sect);
assert(fp->sect >= 1);
assert(fp->sect <= FDD_SPT);
fp->r_ptr = fp->sect - 1;
fp->r_ptr *= FDD_BPS;
fp->r_ptr += fp->track * FDD_SPT * FDD_BPS;
memcpy(fp->img + fp->r_ptr, fp->wbuf, FDD_BPS);
fp->w_ptr = 0;
break;
case 0x0200:
dev_trace(iop, "FDD RECAL\n");
fp->track = 0;
break;
case 0x0300:
fp->track = iop->oreg_a & 0x00ff;
dev_trace(iop, "FDD SEEK track %u\n", fp->track);
assert(fp->track < FDD_TPD);
break;
default:
assert(__LINE__ == 0);
}
callout_dev_is_done(iop, fp->speed);
}
if (IO_ACTION(ioi) == IO_PULSE) {
fp->w_ptr = 0;
}
}
static void * v_matchproto_(new_dev_f)
new_fdd(struct iodev *iop1, struct iodev *iop2)
{
struct io_fdd *fp;
AN(iop1);
AZ(iop2);
fp = calloc(1, sizeof *fp);
AN(fp);
fp->iop = iop1;
fp->speed = 10000;
fp->iop->io_func = dev_fdd_iofunc;
fp->iop->priv = fp;
cpu_add_dev(fp->iop, NULL);
return (fp);
}
static void
fdd_load_save(struct io_fdd *fp, struct cli *cli, int save)
{
int fd;
int e;
ssize_t sz;
if (cli_n_args(cli, 1))
return;
fd = open(cli->av[1],
save ? O_WRONLY | O_CREAT | O_TRUNC : O_RDONLY,
0644);
if (fd < 0) {
cli_error(cli, "Cannot open %s: %s\n",
cli->av[1], strerror(errno));
return;
}
if (save)
sz = write(fd, fp->img, sizeof fp->img);
else
sz = read(fd, fp->img, sizeof fp->img);
e = errno;
AZ(close(fd));
if (sz < 0 || (size_t)sz != sizeof fp->img) {
cli_error(cli, "%s error %s: %s\n",
save ? "Write" : "Read",
cli->av[1], strerror(e));
return;
}
cli->av += 2;
cli->ac -= 2;
}
void v_matchproto_(cli_func_f)
cli_fdd(struct cli *cli)
{
struct io_fdd *fp;
if (cli->help) {
cli_io_help(cli, "RC3650 Floppy disk controller", 1, 0);
cli_printf(cli, "\tload <filename>\n");
cli_printf(cli, "\t\tLoad floppy-image from file\n");
cli_printf(cli, "\tsave <filename>\n");
cli_printf(cli, "\t\tSave floppy-image to file\n");
return;
}
cli->ac--;
cli->av++;
fp = cli_dev_get_unit(cli, "FDD", NULL, new_fdd);
if (fp == NULL)
return;
AN(fp);
while (cli->ac && !cli->status) {
if (cli_dev_trace(fp->iop, cli))
continue;
if (!strcasecmp(*cli->av, "load")) {
fdd_load_save(fp, cli, 0);
return;
}
if (!strcasecmp(*cli->av, "save")) {
fdd_load_save(fp, cli, 1);
return;
}
cli_unknown(cli);
break;
}
}