Mon, 06 Dec 2010 08:27:21 +0000
move edge-sensitive FDC IRQ to main()
philpem@48 | 1 | #include <stdint.h> |
philpem@48 | 2 | #include <stdbool.h> |
philpem@49 | 3 | #include <malloc.h> |
philpem@53 | 4 | #include "musashi/m68k.h" |
philpem@48 | 5 | #include "wd279x.h" |
philpem@48 | 6 | |
philpem@48 | 7 | /// WD2797 command constants |
philpem@48 | 8 | enum { |
philpem@48 | 9 | CMD_MASK = 0xF0, ///< Bit mask to detect command bits |
philpem@48 | 10 | CMD_RESTORE = 0x00, ///< Restore (recalibrate, seek to track 0) |
philpem@48 | 11 | CMD_SEEK = 0x10, ///< Seek to given track |
philpem@48 | 12 | CMD_STEP = 0x20, ///< Step |
philpem@48 | 13 | CMD_STEP_TU = 0x30, ///< Step and update track register |
philpem@48 | 14 | CMD_STEPIN = 0x40, ///< Step In |
philpem@48 | 15 | CMD_STEPIN_TU = 0x50, ///< Step In and update track register |
philpem@48 | 16 | CMD_STEPOUT = 0x60, ///< Step Out |
philpem@48 | 17 | CMD_STEPOUT_TU = 0x70, ///< Step Out and update track register |
philpem@48 | 18 | CMD_READ_SECTOR = 0x80, ///< Read Sector |
philpem@48 | 19 | CMD_READ_SECTOR_MULTI = 0x90, ///< Read Multiple Sectors |
philpem@48 | 20 | CMD_WRITE_SECTOR = 0xA0, ///< Write Sector |
philpem@48 | 21 | CMD_WRITE_SECTOR_MULTI = 0xB0, ///< Write Multiple Sectors |
philpem@48 | 22 | CMD_READ_ADDRESS = 0xC0, ///< Read Address (IDAM contents) |
philpem@48 | 23 | CMD_FORCE_INTERRUPT = 0xD0, ///< Force Interrupt |
philpem@48 | 24 | CMD_READ_TRACK = 0xE0, ///< Read Track |
philpem@48 | 25 | CMD_FORMAT_TRACK = 0xF0 ///< Format Track |
philpem@48 | 26 | }; |
philpem@48 | 27 | |
philpem@48 | 28 | |
philpem@49 | 29 | void wd2797_init(WD2797_CTX *ctx) |
philpem@48 | 30 | { |
philpem@48 | 31 | // track, head and sector unknown |
philpem@49 | 32 | ctx->track = ctx->head = ctx->sector = 0; |
philpem@48 | 33 | |
philpem@49 | 34 | // no IRQ pending |
philpem@76 | 35 | ctx->irq = false; |
philpem@48 | 36 | |
philpem@48 | 37 | // no data available |
philpem@49 | 38 | ctx->data_pos = ctx->data_len = 0; |
philpem@49 | 39 | ctx->data = NULL; |
philpem@49 | 40 | |
philpem@76 | 41 | // Status register clear, not busy; type1 command |
philpem@49 | 42 | ctx->status = 0; |
philpem@76 | 43 | ctx->cmd_has_drq = false; |
philpem@49 | 44 | |
philpem@49 | 45 | // Clear data register |
philpem@49 | 46 | ctx->data_reg = 0; |
philpem@49 | 47 | |
philpem@76 | 48 | // Last step direction = "towards zero" |
philpem@49 | 49 | ctx->last_step_dir = -1; |
philpem@48 | 50 | |
philpem@48 | 51 | // No disc image loaded |
philpem@48 | 52 | ctx->disc_image = NULL; |
philpem@49 | 53 | ctx->geom_secsz = ctx->geom_spt = ctx->geom_heads = ctx->geom_tracks = 0; |
philpem@48 | 54 | } |
philpem@48 | 55 | |
philpem@48 | 56 | |
philpem@49 | 57 | void wd2797_reset(WD2797_CTX *ctx) |
philpem@49 | 58 | { |
philpem@49 | 59 | // track, head and sector unknown |
philpem@49 | 60 | ctx->track = ctx->head = ctx->sector = 0; |
philpem@49 | 61 | |
philpem@49 | 62 | // no IRQ pending |
philpem@76 | 63 | ctx->irq = false; |
philpem@49 | 64 | |
philpem@49 | 65 | // no data available |
philpem@49 | 66 | ctx->data_pos = ctx->data_len = 0; |
philpem@49 | 67 | |
philpem@49 | 68 | // Status register clear, not busy |
philpem@49 | 69 | ctx->status = 0; |
philpem@49 | 70 | |
philpem@49 | 71 | // Clear data register |
philpem@49 | 72 | ctx->data_reg = 0; |
philpem@49 | 73 | |
philpem@49 | 74 | // Last step direction |
philpem@49 | 75 | ctx->last_step_dir = -1; |
philpem@49 | 76 | } |
philpem@49 | 77 | |
philpem@49 | 78 | |
philpem@49 | 79 | void wd2797_done(WD2797_CTX *ctx) |
philpem@49 | 80 | { |
philpem@49 | 81 | // Reset the WD2797 |
philpem@49 | 82 | wd2797_reset(ctx); |
philpem@49 | 83 | |
philpem@49 | 84 | // Free any allocated memory |
philpem@49 | 85 | if (ctx->data) { |
philpem@49 | 86 | free(ctx->data); |
philpem@49 | 87 | ctx->data = NULL; |
philpem@49 | 88 | } |
philpem@49 | 89 | } |
philpem@49 | 90 | |
philpem@49 | 91 | |
philpem@49 | 92 | bool wd2797_get_irq(WD2797_CTX *ctx) |
philpem@48 | 93 | { |
philpem@76 | 94 | return ctx->irq; |
philpem@48 | 95 | } |
philpem@48 | 96 | |
philpem@48 | 97 | |
philpem@49 | 98 | bool wd2797_get_drq(WD2797_CTX *ctx) |
philpem@48 | 99 | { |
philpem@48 | 100 | return (ctx->data_pos < ctx->data_len); |
philpem@48 | 101 | } |
philpem@48 | 102 | |
philpem@48 | 103 | |
philpem@49 | 104 | WD2797_ERR wd2797_load(WD2797_CTX *ctx, FILE *fp, int secsz, int spt, int heads) |
philpem@49 | 105 | { |
philpem@49 | 106 | size_t filesize; |
philpem@49 | 107 | |
philpem@49 | 108 | // Start by finding out how big the image file is |
philpem@49 | 109 | fseek(fp, 0, SEEK_END); |
philpem@49 | 110 | filesize = ftell(fp); |
philpem@49 | 111 | fseek(fp, 0, SEEK_SET); |
philpem@49 | 112 | |
philpem@49 | 113 | // Now figure out how many tracks it contains |
philpem@49 | 114 | int tracks = filesize / secsz / spt / heads; |
philpem@49 | 115 | // Confirm... |
philpem@49 | 116 | if (tracks < 1) { |
philpem@49 | 117 | return WD2797_ERR_BAD_GEOM; |
philpem@49 | 118 | } |
philpem@49 | 119 | |
philpem@49 | 120 | // Allocate enough memory to store one disc track |
philpem@49 | 121 | if (ctx->data) { |
philpem@49 | 122 | free(ctx->data); |
philpem@49 | 123 | } |
philpem@49 | 124 | ctx->data = malloc(secsz * spt); |
philpem@49 | 125 | if (!ctx->data) |
philpem@49 | 126 | return WD2797_ERR_NO_MEMORY; |
philpem@49 | 127 | |
philpem@49 | 128 | // Load the image and the geometry data |
philpem@49 | 129 | ctx->disc_image = fp; |
philpem@49 | 130 | ctx->geom_tracks = tracks; |
philpem@49 | 131 | ctx->geom_secsz = secsz; |
philpem@49 | 132 | ctx->geom_heads = heads; |
philpem@49 | 133 | ctx->geom_spt = spt; |
philpem@49 | 134 | |
philpem@49 | 135 | return WD2797_ERR_OK; |
philpem@49 | 136 | } |
philpem@49 | 137 | |
philpem@49 | 138 | |
philpem@49 | 139 | void wd2797_unload(WD2797_CTX *ctx) |
philpem@49 | 140 | { |
philpem@49 | 141 | // Free memory buffer |
philpem@49 | 142 | if (ctx->data) { |
philpem@49 | 143 | free(ctx->data); |
philpem@49 | 144 | ctx->data = NULL; |
philpem@49 | 145 | } |
philpem@49 | 146 | |
philpem@49 | 147 | // Clear file pointer |
philpem@49 | 148 | ctx->disc_image = NULL; |
philpem@49 | 149 | |
philpem@49 | 150 | // Clear the disc geometry |
philpem@49 | 151 | ctx->geom_tracks = ctx->geom_secsz = ctx->geom_spt = ctx->geom_heads = 0; |
philpem@49 | 152 | } |
philpem@49 | 153 | |
philpem@49 | 154 | |
philpem@49 | 155 | uint8_t wd2797_read_reg(WD2797_CTX *ctx, uint8_t addr) |
philpem@48 | 156 | { |
philpem@48 | 157 | uint8_t temp = 0; |
philpem@48 | 158 | |
philpem@48 | 159 | switch (addr & 0x03) { |
philpem@49 | 160 | case WD2797_REG_STATUS: // Status register |
philpem@48 | 161 | // Read from status register clears IRQ |
philpem@76 | 162 | ctx->irq = false; |
philpem@48 | 163 | |
philpem@48 | 164 | // Get current status flags (set by last command) |
philpem@48 | 165 | // DRQ bit |
philpem@53 | 166 | if (ctx->cmd_has_drq) { |
philpem@53 | 167 | printf("\tWDFDC rd sr, has drq, pos=%lu len=%lu\n", ctx->data_pos, ctx->data_len); |
philpem@53 | 168 | temp = ctx->status & ~0x03; |
philpem@48 | 169 | temp |= (ctx->data_pos < ctx->data_len) ? 0x02 : 0x00; |
philpem@53 | 170 | } else { |
philpem@53 | 171 | temp = ctx->status & ~0x01; |
philpem@53 | 172 | } |
philpem@48 | 173 | // FDC is busy if there is still data in the buffer |
philpem@48 | 174 | temp |= (ctx->data_pos < ctx->data_len) ? 0x01 : 0x00; // if data in buffer, then DMA hasn't copied it yet, and we're still busy! |
philpem@49 | 175 | // TODO: also if seek delay / read delay hasn't passed (but that's for later) |
philpem@48 | 176 | return temp; |
philpem@48 | 177 | |
philpem@49 | 178 | case WD2797_REG_TRACK: // Track register |
philpem@48 | 179 | return ctx->track; |
philpem@48 | 180 | |
philpem@49 | 181 | case WD2797_REG_SECTOR: // Sector register |
philpem@48 | 182 | return ctx->sector; |
philpem@48 | 183 | |
philpem@49 | 184 | case WD2797_REG_DATA: // Data register |
philpem@48 | 185 | // If there's data in the buffer, return it. Otherwise return 0xFF. |
philpem@48 | 186 | if (ctx->data_pos < ctx->data_len) { |
philpem@53 | 187 | // set IRQ if this is the last data byte |
philpem@53 | 188 | if (ctx->data_pos == (ctx->data_len-1)) { |
philpem@76 | 189 | // Set IRQ |
philpem@76 | 190 | ctx->irq = true; |
philpem@53 | 191 | } |
philpem@48 | 192 | // return data byte and increment pointer |
philpem@48 | 193 | return ctx->data[ctx->data_pos++]; |
philpem@48 | 194 | } else { |
philpem@53 | 195 | // command finished |
philpem@48 | 196 | return 0xff; |
philpem@48 | 197 | } |
philpem@48 | 198 | |
philpem@48 | 199 | default: |
philpem@48 | 200 | // shut up annoying compilers which don't recognise unreachable code when they see it |
philpem@48 | 201 | // (here's looking at you, gcc!) |
philpem@48 | 202 | return 0xff; |
philpem@48 | 203 | } |
philpem@48 | 204 | } |
philpem@48 | 205 | |
philpem@48 | 206 | |
philpem@49 | 207 | void wd2797_write_reg(WD2797_CTX *ctx, uint8_t addr, uint8_t val) |
philpem@48 | 208 | { |
philpem@48 | 209 | uint8_t cmd = val & CMD_MASK; |
philpem@48 | 210 | size_t lba; |
philpem@48 | 211 | bool is_type1 = false; |
philpem@49 | 212 | int temp; |
philpem@48 | 213 | |
philpem@53 | 214 | m68k_end_timeslice(); |
philpem@53 | 215 | |
philpem@48 | 216 | switch (addr) { |
philpem@49 | 217 | case WD2797_REG_COMMAND: // Command register |
philpem@48 | 218 | // write to command register clears interrupt request |
philpem@76 | 219 | ctx->irq = false; |
philpem@48 | 220 | |
philpem@48 | 221 | // Is the drive ready? |
philpem@48 | 222 | if (ctx->disc_image == NULL) { |
philpem@48 | 223 | // No disc image, thus the drive is busy. |
philpem@48 | 224 | ctx->status = 0x80; |
philpem@48 | 225 | return; |
philpem@48 | 226 | } |
philpem@48 | 227 | |
philpem@48 | 228 | // Handle Type 1 commands |
philpem@48 | 229 | switch (cmd) { |
philpem@48 | 230 | case CMD_RESTORE: |
philpem@48 | 231 | // Restore. Set track to 0 and throw an IRQ. |
philpem@48 | 232 | is_type1 = true; |
philpem@48 | 233 | ctx->track = 0; |
philpem@48 | 234 | break; |
philpem@48 | 235 | |
philpem@48 | 236 | case CMD_SEEK: |
philpem@48 | 237 | // Seek. Seek to the track specced in the Data Register. |
philpem@48 | 238 | is_type1 = true; |
philpem@48 | 239 | if (ctx->data_reg < ctx->geom_tracks) { |
philpem@48 | 240 | ctx->track = ctx->data_reg; |
philpem@48 | 241 | } else { |
philpem@48 | 242 | // Seek error. :( |
philpem@48 | 243 | ctx->status = 0x10; |
philpem@48 | 244 | } |
philpem@48 | 245 | |
philpem@48 | 246 | case CMD_STEP: |
philpem@48 | 247 | // TODO! deal with trk0! |
philpem@48 | 248 | // Need to keep a copy of the track register; when it hits 0, set the TRK0 flag. |
philpem@48 | 249 | is_type1 = true; |
philpem@48 | 250 | break; |
philpem@48 | 251 | |
philpem@48 | 252 | case CMD_STEPIN: |
philpem@48 | 253 | case CMD_STEPOUT: |
philpem@48 | 254 | // TODO! deal with trk0! |
philpem@48 | 255 | // Need to keep a copy of the track register; when it hits 0, set the TRK0 flag. |
philpem@48 | 256 | if (cmd == CMD_STEPIN) { |
philpem@48 | 257 | ctx->last_step_dir = 1; |
philpem@48 | 258 | } else { |
philpem@48 | 259 | ctx->last_step_dir = -1; |
philpem@48 | 260 | } |
philpem@48 | 261 | is_type1 = true; |
philpem@48 | 262 | break; |
philpem@48 | 263 | |
philpem@48 | 264 | case CMD_STEP_TU: |
philpem@48 | 265 | case CMD_STEPIN_TU: |
philpem@48 | 266 | case CMD_STEPOUT_TU: |
philpem@48 | 267 | // if this is a Step In or Step Out cmd, set the step-direction |
philpem@48 | 268 | if (cmd == CMD_STEPIN_TU) { |
philpem@48 | 269 | ctx->last_step_dir = 1; |
philpem@48 | 270 | } else if (cmd == CMD_STEPOUT_TU) { |
philpem@48 | 271 | ctx->last_step_dir = -1; |
philpem@48 | 272 | } |
philpem@48 | 273 | |
philpem@48 | 274 | // Seek one step in the last direction used. |
philpem@48 | 275 | ctx->track += ctx->last_step_dir; |
philpem@48 | 276 | if (ctx->track < 0) ctx->track = 0; |
philpem@48 | 277 | if (ctx->track >= ctx->geom_tracks) { |
philpem@48 | 278 | // Seek past end of disc... that'll be a Seek Error then. |
philpem@48 | 279 | ctx->status = 0x10; |
philpem@48 | 280 | ctx->track = ctx->geom_tracks - 1; |
philpem@48 | 281 | } |
philpem@48 | 282 | is_type1 = true; |
philpem@48 | 283 | break; |
philpem@48 | 284 | |
philpem@48 | 285 | default: |
philpem@48 | 286 | break; |
philpem@48 | 287 | } |
philpem@48 | 288 | |
philpem@48 | 289 | if (is_type1) { |
philpem@48 | 290 | // Terminate any sector reads or writes |
philpem@48 | 291 | ctx->data_len = ctx->data_pos = 0; |
philpem@48 | 292 | |
philpem@48 | 293 | // No DRQ bit for these commands. |
philpem@48 | 294 | ctx->cmd_has_drq = false; |
philpem@48 | 295 | |
philpem@48 | 296 | // Type1 status byte... |
philpem@48 | 297 | ctx->status = 0; |
philpem@48 | 298 | // S7 = Not Ready. Command executed, therefore the drive was ready... :) |
philpem@48 | 299 | // S6 = Write Protect. TODO: add this |
philpem@48 | 300 | // S5 = Head Loaded. For certain emulation-related reasons, the heads are always loaded... |
philpem@48 | 301 | ctx->status |= 0x20; |
philpem@48 | 302 | // S4 = Seek Error. Not bloody likely if we got down here...! |
philpem@48 | 303 | // S3 = CRC Error. Not gonna happen on a disc image! |
philpem@48 | 304 | // S2 = Track 0 |
philpem@48 | 305 | ctx->status |= (ctx->track == 0) ? 0x04 : 0x00; |
philpem@48 | 306 | // S1 = Index Pulse. TODO -- need periodics to emulate this |
philpem@48 | 307 | // S0 = Busy. We just exec'd the command, thus we're not busy. |
philpem@48 | 308 | // TODO: Set a timer for seeks, and ONLY clear BUSY when that timer expires. Need periodics for that. |
philpem@48 | 309 | |
philpem@76 | 310 | // Set IRQ |
philpem@76 | 311 | ctx->irq = true; |
philpem@48 | 312 | return; |
philpem@48 | 313 | } |
philpem@48 | 314 | |
philpem@48 | 315 | // That's the Type 1 (seek) commands sorted. Now for the others. |
philpem@48 | 316 | |
philpem@53 | 317 | // All these commands return the DRQ bit... |
philpem@53 | 318 | ctx->cmd_has_drq = true; |
philpem@53 | 319 | |
philpem@48 | 320 | // If drive isn't ready, then set status B7 and exit |
philpem@48 | 321 | if (ctx->disc_image == NULL) { |
philpem@48 | 322 | ctx->status = 0x80; |
philpem@48 | 323 | return; |
philpem@48 | 324 | } |
philpem@48 | 325 | |
philpem@48 | 326 | // If this is a Write command, check write protect status too |
philpem@48 | 327 | // TODO! |
philpem@48 | 328 | if (false) { |
philpem@48 | 329 | // Write protected disc... |
philpem@48 | 330 | if ((cmd == CMD_WRITE_SECTOR) || (cmd == CMD_WRITE_SECTOR_MULTI) || (cmd == CMD_FORMAT_TRACK)) { |
philpem@48 | 331 | // Set Write Protect bit and bail. |
philpem@48 | 332 | ctx->status = 0x40; |
philpem@48 | 333 | |
philpem@76 | 334 | // Set IRQ |
philpem@76 | 335 | ctx->irq = true; |
philpem@48 | 336 | |
philpem@48 | 337 | return; |
philpem@48 | 338 | } |
philpem@48 | 339 | } |
philpem@48 | 340 | |
philpem@48 | 341 | // Disc is ready to go. Parse the command word. |
philpem@48 | 342 | switch (cmd) { |
philpem@48 | 343 | case CMD_READ_ADDRESS: |
philpem@48 | 344 | // Read Address |
philpem@54 | 345 | ctx->head = (val & 0x02) ? 1 : 0; |
philpem@48 | 346 | |
philpem@48 | 347 | // reset data pointers |
philpem@48 | 348 | ctx->data_pos = ctx->data_len = 0; |
philpem@48 | 349 | |
philpem@48 | 350 | // load data buffer |
philpem@48 | 351 | ctx->data[ctx->data_len++] = ctx->track; |
philpem@48 | 352 | ctx->data[ctx->data_len++] = ctx->head; |
philpem@48 | 353 | ctx->data[ctx->data_len++] = ctx->sector; |
philpem@48 | 354 | switch (ctx->geom_secsz) { |
philpem@48 | 355 | case 128: ctx->data[ctx->data_len++] = 0; break; |
philpem@48 | 356 | case 256: ctx->data[ctx->data_len++] = 1; break; |
philpem@48 | 357 | case 512: ctx->data[ctx->data_len++] = 2; break; |
philpem@48 | 358 | case 1024: ctx->data[ctx->data_len++] = 3; break; |
philpem@48 | 359 | default: ctx->data[ctx->data_len++] = 0xFF; break; // TODO: deal with invalid values better |
philpem@48 | 360 | } |
philpem@48 | 361 | ctx->data[ctx->data_len++] = 0; // TODO: IDAM CRC! |
philpem@48 | 362 | ctx->data[ctx->data_len++] = 0; |
philpem@48 | 363 | |
philpem@53 | 364 | ctx->status = 0; |
philpem@48 | 365 | // B6, B5 = 0 |
philpem@48 | 366 | // B4 = Record Not Found. We're not going to see this... FIXME-not emulated |
philpem@48 | 367 | // B3 = CRC Error. Not possible. |
philpem@48 | 368 | // B2 = Lost Data. Caused if DRQ isn't serviced in time. FIXME-not emulated |
philpem@48 | 369 | // B1 = DRQ. Data request. |
philpem@48 | 370 | ctx->status |= (ctx->data_pos < ctx->data_len) ? 0x02 : 0x00; |
philpem@48 | 371 | break; |
philpem@48 | 372 | |
philpem@48 | 373 | case CMD_READ_SECTOR: |
philpem@48 | 374 | case CMD_READ_SECTOR_MULTI: |
philpem@54 | 375 | ctx->head = (val & 0x02) ? 1 : 0; |
philpem@54 | 376 | printf("WD279X: READ SECTOR cmd=%02X chs=%d:%d:%d\n", cmd, ctx->track, ctx->head, ctx->sector); |
philpem@48 | 377 | // Read Sector or Read Sector Multiple |
philpem@48 | 378 | // reset data pointers |
philpem@48 | 379 | ctx->data_pos = ctx->data_len = 0; |
philpem@48 | 380 | |
philpem@49 | 381 | // Calculate number of sectors to read from disc |
philpem@49 | 382 | if (cmd == CMD_READ_SECTOR_MULTI) |
philpem@49 | 383 | temp = ctx->geom_spt; |
philpem@49 | 384 | else |
philpem@49 | 385 | temp = 1; |
philpem@48 | 386 | |
philpem@49 | 387 | for (int i=0; i<temp; i++) { |
philpem@49 | 388 | // Calculate the LBA address of the required sector |
philpem@51 | 389 | lba = ((((ctx->track * ctx->geom_heads) + ctx->head) * ctx->geom_spt) + ((ctx->sector + i - 1) % ctx->geom_spt)) * ctx->geom_secsz; |
philpem@53 | 390 | printf("\tREAD lba = %lu\n", lba); |
philpem@49 | 391 | |
philpem@49 | 392 | // Read the sector from the file |
philpem@49 | 393 | fseek(ctx->disc_image, lba, SEEK_SET); |
philpem@49 | 394 | ctx->data_len += fread(&ctx->data[ctx->data_len], 1, ctx->geom_secsz, ctx->disc_image); |
philpem@53 | 395 | printf("\tREAD len=%lu, pos=%lu, ssz=%d\n", ctx->data_len, ctx->data_pos, ctx->geom_secsz); |
philpem@49 | 396 | // TODO: check fread return value! if < secsz, BAIL! (call it a crc error or secnotfound maybe? also log to stderr) |
philpem@49 | 397 | } |
philpem@48 | 398 | |
philpem@53 | 399 | ctx->status = 0; |
philpem@48 | 400 | // B6 = 0 |
philpem@48 | 401 | // B5 = Record Type -- 1 = deleted, 0 = normal. We can't emulate anything but normal data blocks. |
philpem@48 | 402 | // B4 = Record Not Found. We're not going to see this... FIXME-not emulated |
philpem@48 | 403 | // B3 = CRC Error. Not possible. |
philpem@48 | 404 | // B2 = Lost Data. Caused if DRQ isn't serviced in time. FIXME-not emulated |
philpem@48 | 405 | // B1 = DRQ. Data request. |
philpem@48 | 406 | ctx->status |= (ctx->data_pos < ctx->data_len) ? 0x02 : 0x00; |
philpem@48 | 407 | break; |
philpem@48 | 408 | |
philpem@48 | 409 | case CMD_READ_TRACK: |
philpem@48 | 410 | // Read Track |
philpem@49 | 411 | // TODO! implement this |
philpem@54 | 412 | ctx->head = (val & 0x02) ? 1 : 0; |
philpem@53 | 413 | ctx->status = 0; |
philpem@48 | 414 | // B6, B5, B4, B3 = 0 |
philpem@48 | 415 | // B2 = Lost Data. Caused if DRQ isn't serviced in time. FIXME-not emulated |
philpem@48 | 416 | // B1 = DRQ. Data request. |
philpem@48 | 417 | ctx->status |= (ctx->data_pos < ctx->data_len) ? 0x02 : 0x00; |
philpem@48 | 418 | break; |
philpem@48 | 419 | |
philpem@48 | 420 | case CMD_WRITE_SECTOR: |
philpem@48 | 421 | case CMD_WRITE_SECTOR_MULTI: |
philpem@48 | 422 | // Write Sector or Write Sector Multiple |
philpem@48 | 423 | |
philpem@54 | 424 | ctx->head = (val & 0x02) ? 1 : 0; |
philpem@48 | 425 | // reset data pointers |
philpem@48 | 426 | ctx->data_pos = ctx->data_len = 0; |
philpem@48 | 427 | |
philpem@48 | 428 | // TODO: set "write pending" flag, and write LBA, and go from there. |
philpem@48 | 429 | |
philpem@53 | 430 | ctx->status = 0; |
philpem@48 | 431 | // B6 = Write Protect. FIXME -- emulate this! |
philpem@48 | 432 | // B5 = 0 |
philpem@48 | 433 | // B4 = Record Not Found. We're not going to see this... FIXME-not emulated |
philpem@48 | 434 | // B3 = CRC Error. Not possible. |
philpem@48 | 435 | // B2 = Lost Data. Caused if DRQ isn't serviced in time. FIXME-not emulated |
philpem@48 | 436 | // B1 = DRQ. Data request. |
philpem@48 | 437 | ctx->status |= (ctx->data_pos < ctx->data_len) ? 0x02 : 0x00; |
philpem@48 | 438 | break; |
philpem@48 | 439 | |
philpem@48 | 440 | case CMD_FORMAT_TRACK: |
philpem@48 | 441 | // Write Track (aka Format Track) |
philpem@54 | 442 | ctx->head = (val & 0x02) ? 1 : 0; |
philpem@53 | 443 | ctx->status = 0; |
philpem@48 | 444 | // B6 = Write Protect. FIXME -- emulate this! |
philpem@48 | 445 | // B5, B4, B3 = 0 |
philpem@48 | 446 | // B2 = Lost Data. Caused if DRQ isn't serviced in time. FIXME-not emulated |
philpem@48 | 447 | // B1 = DRQ. Data request. |
philpem@48 | 448 | ctx->status |= (ctx->data_pos < ctx->data_len) ? 0x02 : 0x00; |
philpem@48 | 449 | break; |
philpem@48 | 450 | |
philpem@48 | 451 | case CMD_FORCE_INTERRUPT: |
philpem@48 | 452 | // Force Interrupt... |
philpem@49 | 453 | // Terminates current operation and sends an interrupt |
philpem@49 | 454 | // TODO! |
philpem@53 | 455 | ctx->status = 0; |
philpem@49 | 456 | ctx->data_pos = ctx->data_len = 0; |
philpem@76 | 457 | // Set IRQ |
philpem@76 | 458 | ctx->irq = true; |
philpem@48 | 459 | break; |
philpem@48 | 460 | } |
philpem@48 | 461 | break; |
philpem@48 | 462 | |
philpem@49 | 463 | case WD2797_REG_TRACK: // Track register |
philpem@48 | 464 | ctx->track = val; |
philpem@48 | 465 | break; |
philpem@48 | 466 | |
philpem@49 | 467 | case WD2797_REG_SECTOR: // Sector register |
philpem@48 | 468 | ctx->sector = val; |
philpem@48 | 469 | break; |
philpem@48 | 470 | |
philpem@49 | 471 | case WD2797_REG_DATA: // Data register |
philpem@48 | 472 | // Save the value written into the data register |
philpem@48 | 473 | ctx->data_reg = val; |
philpem@48 | 474 | |
philpem@48 | 475 | // If we're processing a write command, and there's space in the |
philpem@48 | 476 | // buffer, allow the write. |
philpem@48 | 477 | if (ctx->data_pos < ctx->data_len) { |
philpem@53 | 478 | // set IRQ if this is the last data byte |
philpem@53 | 479 | if (ctx->data_pos == (ctx->data_len-1)) { |
philpem@76 | 480 | // Set IRQ |
philpem@76 | 481 | ctx->irq = true; |
philpem@53 | 482 | } |
philpem@53 | 483 | |
philpem@48 | 484 | // store data byte and increment pointer |
philpem@48 | 485 | ctx->data[ctx->data_pos++] = val; |
philpem@48 | 486 | } |
philpem@48 | 487 | break; |
philpem@48 | 488 | } |
philpem@48 | 489 | } |
philpem@48 | 490 |