summaryrefslogtreecommitdiffstats
path: root/disk.c
blob: 95a7d2f133e75b5edcb242b748b016349cc0a6b4 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * ABC80 simulated disk
 */

#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include "z80.h"

#define NOTTHERE 0
#define READONLY 0
#define TRACE 0
#define INTERLEAVE 0

/* This is the interpretation of an "out" command */
enum out_state {
  disk_need_init,
  disk_k0,
  disk_k1,
  disk_k2,
  disk_k3,
  disk_upload,
  disk_download
};

struct ctl_state {
  enum out_state state;
  uint8_t k[4];
  unsigned int secperclust;
  unsigned int sectors;
  uint8_t ilmsk, ilfac;		/* Interlacing parameters */
  uint8_t new;			/* "New addressing" */
  const char name[3];
  int out_ptr;			/* Pointer within buffer for out data */
  int in_ptr;			/* Pointer within buffer for in data */
  int status;			/* Primary status */
  int aux_status;		/* Auxilliary status */
  int notready_ctr;		/* How many times are we not ready? */
  FILE *files[8];		/* File for this unit */
  unsigned char buf[4][256];	/* 4 buffers @ 256 bytes */
};

static struct ctl_state mo_state =
  {
    .secperclust = 1,
    .sectors     = 40*1*16,
#if INTERLEAVE
    .ilmsk       = 15,
    .ilfac       = 7,
#endif
    .name        = "mo"
  };
static struct ctl_state mf_state =
  {
    .secperclust = 4,
    .sectors     = 80*2*16,
    .name        = "mf"
  };
static struct ctl_state sf_state =
  {
    .secperclust = 4,
    .sectors     = (77*2-1)*26,	/* Spår 0, sida 0 används ej */
    .name        = "sf"
  };
static struct ctl_state hd_state =
  {
    .secperclust = 32,
    .new         = 1,		/* Actually irrelevant when secperclust = 32 */
    .sectors     = 238*8*32,
    .name        = "hd"
  };

static struct ctl_state * const sel_to_state[64] =
  {
    [36] = &hd_state,
    [44] = &mf_state,
    [45] = &mo_state,
    [46] = &sf_state,
  };

static inline int cur_sector(struct ctl_state *state)
{
  uint8_t k2 = state->k[2], k3 = state->k[3];

  if (state->new)
    return (k2 << 8) + k3;
  else
    return (((k2 << 3) + (k3 >> 5))*state->secperclust) + (k3 & 31);
}

static inline int file_pos_valid(struct ctl_state *state)
{
  return cur_sector(state) < state->sectors;
}

static inline int file_pos(struct ctl_state *state)
{
  unsigned int ilmsk = state->ilmsk;
  unsigned int sector = cur_sector(state);

  sector = (sector & ~ilmsk) | ((sector*state->ilfac) & ilmsk);

  return sector << 8;
}

static void disk_reset_state(struct ctl_state *state)
{
  state->state = disk_k0;
  state->status = state->aux_status = 0;
  state->in_ptr  = -1;
  state->out_ptr = 0;
  state->notready_ctr = 4;
}

static void disk_init(struct ctl_state *state)
{
  char filename[64];
  int i;

  /* If any of these don't exist we simply report device not ready */
  for (i = 0; i < 8; i++) {
    snprintf(filename, sizeof filename, "abcdisk/%s%d", state->name, i);
    state->files[i] = fopen(filename, "r+b");
    if (!state->files[i])
      state->files[i] = fopen(filename, "rb"); /* Try open readonly */
  }
  disk_reset_state(state);
}

static void do_next_command(struct ctl_state *state)
{
  FILE *file = state->files[state->k[1] & 7];

  if ( state->k[0] & 0x01 ) {
    /* READ SECTOR */
    fseek(file, file_pos(state), SEEK_SET);
    fread(state->buf[state->k[1] >> 6], 1, 256, file);
    state->k[0] &= ~0x01;	/* Command done */
  }
  if ( state->k[0] & 0x02 ) {
    /* SECTOR TO HOST */
    state->in_ptr = 0;
    state->state = disk_download;
    state->k[0] &= ~0x02;	/* Command done */
    return;
  }
  if ( state->k[0] & 0x04 ) {
    /* SECTOR FROM HOST */
    state->state = disk_upload;
    state->out_ptr = 0;
    state->k[0] &= ~0x04;	/* Command done */
    return;
  }
  if ( state->k[0] & 0x08 ) {
    /* WRITE SECTOR */
    clearerr(file);
    fseek(file, file_pos(state), SEEK_SET);
    fwrite(state->buf[state->k[1] >> 6], 1, 256, file);
    if (ferror(file)) {
      state->status = 0x08; /* Error */
      state->aux_status = 0x40; /* Write protect */
    }
    state->k[0] &= ~0x08;	/* Command done */
  }
  state->state = disk_k0;
}

void disk_reset(void)
{
  int i;
  struct ctl_state *state;
  for (i = 0; i < 64; i++) {
    state = sel_to_state[i];
    if (state && state->state != disk_need_init)
      disk_reset_state(state);
  }
}

void disk_out(int sel, int port, int value)
{
  struct ctl_state *state = sel_to_state[sel];

  if (!state)
    return;			/* Not a disk drive */

  if ( state->state == disk_need_init )
    disk_init(state);

  switch ( port ) {
  case 0:
    switch ( state->state ) {
    case disk_k0 ... disk_k2:
      state->status = state->aux_status = 0;
      state->k[state->state - disk_k0] = value;
      state->state++;
      break;
    case disk_k3:
      state->status = state->aux_status = 0;
      state->k[3] = value;
      state->state = disk_k0;

#if TRACE
      printf("%s%d: command %02X %02X %02X %02X\n",
	     state->name, state->k[1] & 7,
	     state->k[0], state->k[1], state->k[2], state->k[3]);
      printf("PC = %04X  BC = %04X  DE = %04X  HL = %04X\n",
	     REG_PC, REG_BC, REG_DE, REG_HL);
#endif 

      /* Bad drive/sector? */
      if ( !state->files[state->k[1] & 7] ) {
	state->status = 0x08;	/* Error */
	state->aux_status = 0x80; /* Device not ready */
      } else if ( !file_pos_valid(state) ) {
	state->status = 0x08;	/* Error */
	state->aux_status = 0x10; /* Seek error */
      } else {
	do_next_command(state);
      }
      break;
    case disk_upload:
      state->buf[state->k[1] >> 6][state->out_ptr++] = value;
#if TRACE
	  printf("%02X", value);
#endif
      if ( state->out_ptr >= 256 ) {
#if TRACE
      printf("\nPC = %04X  BC = %04X  DE = %04X  HL = %04X\n",
	     REG_PC, REG_BC, REG_DE, REG_HL);
#endif
	do_next_command(state);
      }
      break;
    case disk_download:
      break;
    }
    break;

  case 2:			/* Start command */
  case 4:			/* Reset */
#if TRACE
    printf("OUT %d/%d : ", sel, port);
    printf("PC = %04X  BC = %04X  DE = %04X  HL = %04X\n",
	   REG_PC, REG_BC, REG_DE, REG_HL);
#endif
    disk_reset();
    break;
    
  default:
    /* Nothing */
    break;
  }
}

int disk_in(int sel, int port)
{
  struct ctl_state *state = sel_to_state[sel];
  uint8_t v = 0xff;

  if (!state)
    return 0xff;		/* Not a disk drive */

  if ( state->state == disk_need_init )
    disk_init(state);

  switch ( port ) {
  case 0:
    if ( state->in_ptr >= 0 ) {
      v = state->buf[state->k[1] >> 6][state->in_ptr++];
      if ( state->in_ptr >= 256 ) {
	state->in_ptr = -1;
	do_next_command(state);
      }
    } else {
      v = state->aux_status;
    }
    break;

  case 1:			/* Controller status */
    if (state->notready_ctr) {
      state->notready_ctr--;
      v = 0x80;
    } else {
      v = 0x01 | state->status | ((state->state == disk_k0) ? 0x80 : 0);
    }
    break;

  default:
    break;
  }

#if TRACE
  printf("IN %d/%d: %02X : ", sel, port, v);
  printf("PC = %04X  BC = %04X  DE = %04X  HL = %04X\n",
	 REG_PC, REG_BC, REG_DE, REG_HL);
#endif
  return v;
}