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