All files / src filestore.js

9.18% Statements 35/381
3.97% Branches 7/176
35.71% Functions 5/14
9.51% Lines 35/368

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657                6x 6x 6x 6x 6x 6x 6x 6x   6x 6x 6x       29x           29x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             6x   6x 6x 6x 6x 6x 6x 6x       29x 29x 29x         29x 29x   29x   29x 29x 29x   29x   29x   29x 29x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             29x                                                        
// Level-3 file server emulator ported from FSEM2
// https://github.com/mmbeeb/FSEM
 
import { ReceiveBlock, EconetPacket } from "./econet.js";
import { loadData } from "./loader.js";
 
export class Filestore {
    constructor(cpu, econet) {
        this.cpu = cpu;
        this.econet = econet;
        this.scsi = [];
        this.l3fs = [];
        this.pollCount = 0;
        this.ram = new Uint8Array((64 + 32) * 1024);
        this.emulationSpeed = 0;
        this.logTemp = "";
 
        this.A = this.X = this.Y = this.SP = this.M = 0;
        this.PC = this.XPC = this.L = this.R = 0;
        this.N = this.V = this.Z = this.C = this.F = 0;
    }
 
    GBYTE() {
        return this.ram[this.PC++];
    }
    GWORD() {
        return this.ram[this.PC++] | (this.ram[this.PC++] << 8);
    }
    WORD(l) {
        return this.ram[l] | (this.ram[l + 1] << 8);
    }
    PUSH(m) {
        this.ram[0x100 + this.SP] = m;
        this.SP--;
        this.SP &= 0xff;
    }
 
    PULL() {
        this.SP++;
        this.SP &= 0xff;
        return this.ram[0x100 + this.SP];
    }
 
    NZ(v) {
        v &= 0xff;
        this.Z = 0 | (v === 0x00);
        this.N = 0 | (v >= 0x80);
        return v;
    }
 
    oswrch(char) {
        if (this.emulationSpeed === 0) {
            // During startup, run the FS emulation at full speed and log output
            if (char >= 32 && char <= 127) {
                this.logTemp += String.fromCharCode(char);
            }
 
            if (char === 13) {
                console.log("Filestore: " + this.logTemp.trim());
                if (this.logTemp.includes("Starting")) {
                    this.emulationSpeed = 20; // Once started up, we can slow down the emulation considerably
                }
                this.logTemp = "";
            }
        }
    }
 
    osword() {
        let p = (this.Y << 8) | this.X;
        switch (this.A) {
            case 0x0: {
                // Read line (put '1' into buffer, Drives/stations = 1)
                let k = this.ram[p] | (this.ram[p + 1] << 8);
                this.ram[k] = 49; // '1'
                this.ram[k + 1] = 13;
                this.Y = 1;
                this.C = 0;
 
                this.oswrch(49); // '1'
                break;
            }
 
            case 0x0e: {
                const current = new Date();
                this.ram[p] = current.getFullYear() - 2000;
                this.ram[p + 1] = current.getMonth() + 1;
                this.ram[p + 2] = current.getDate();
                this.ram[p + 3] = current.getDay() + 1;
                this.ram[p + 4] = current.getHours();
                this.ram[p + 5] = current.getMinutes();
                this.ram[p + 6] = current.getSeconds();
                break;
            }
 
            case 0x10: {
                // Transmit
                let stationId = this.ram[p + 2];
 
                if (stationId !== 0xff) {
                    let bufstart =
                        (this.ram[p + 4] |
                            (this.ram[p + 5] << 8) |
                            (this.ram[p + 6] << 16) |
                            (this.ram[p + 7] << 24)) >>>
                        0;
                    let bufend =
                        (this.ram[p + 8] |
                            (this.ram[p + 9] << 8) |
                            (this.ram[p + 10] << 16) |
                            (this.ram[p + 11] << 24)) >>>
                        0;
                    let length = bufend - bufstart;
 
                    if (bufstart >= 0x10000) bufstart = (bufstart & 0xffff) | 0x10000;
 
                    this.econet.serverTx = new EconetPacket(stationId, 0, 254, 0);
                    this.econet.serverTx.controlFlag = this.ram[p];
                    this.econet.serverTx.port = this.ram[p + 1];
 
                    for (let i = 0; i < length; i++) {
                        this.econet.serverTx.buffer[i + 4] = this.ram[bufstart + i];
                    }
                    this.econet.serverTx.bytesInBuffer = 4 + length;
                }
                break;
            }
 
            case 0x11: {
                // Receive
                if (this.ram[p + 0] === 0) {
                    // Create new receive block
                    let rxBuffer = new ReceiveBlock(
                        this.econet.nextReceiveBlockNumber,
                        this.ram[p + 1],
                        this.ram[p + 2],
                        (this.ram[p + 5] |
                            (this.ram[p + 6] << 8) |
                            (this.ram[p + 7] << 16) |
                            (this.ram[p + 8] << 24)) >>>
                            0,
                        (this.ram[p + 9] |
                            (this.ram[p + 10] << 8) |
                            (this.ram[p + 11] << 16) |
                            (this.ram[p + 12] << 24)) >>>
                            0,
                    );
 
                    this.econet.receiveBlocks.push(rxBuffer);
                    this.ram[p] = this.econet.nextReceiveBlockNumber;
                    this.econet.nextReceiveBlockNumber++;
                    this.econet.nextReceiveBlockNumber &= 0xff;
                    if (this.econet.nextReceiveBlockNumber === 0) {
                        this.econet.nextReceiveBlockNumber = 1;
                    }
 
                    //console.log("Filestore: new receive block " + rxBuffer.id + " " + rxBuffer.receivePort.toString(16) + " " + rxBuffer.bufferStart.toString(16) + " " + rxBuffer.bufferEnd.toString(16));
                } // Read and delete receive block
                else {
                    let rxblock = this.econet.receiveBlocks.find((e) => e.id === this.ram[p + 0]);
                    if (rxblock.bufferStart + rxblock.data.bytesInBuffer - 4 < rxblock.bufferEnd) {
                        rxblock.bufferEnd = rxblock.bufferStart + rxblock.data.bytesInBuffer - 4;
                    }
                    this.ram[p + 1] = rxblock.controlFlag;
                    this.ram[p + 2] = rxblock.receivePort;
                    this.ram[p + 3] = rxblock.stationId;
                    this.ram[p + 4] = 0;
                    this.ram[p + 5] = rxblock.bufferStart & 0xff;
                    this.ram[p + 6] = (rxblock.bufferStart >>> 8) & 0xff;
                    this.ram[p + 7] = (rxblock.bufferStart >>> 16) & 0xff;
                    this.ram[p + 8] = (rxblock.bufferStart >>> 24) & 0xff;
                    this.ram[p + 9] = rxblock.bufferEnd & 0xff;
                    this.ram[p + 10] = (rxblock.bufferEnd >>> 8) & 0xff;
                    this.ram[p + 11] = (rxblock.bufferEnd >>> 16) & 0xff;
                    this.ram[p + 12] = (rxblock.bufferEnd >>> 24) & 0xff;
 
                    // Copy buffer contents to FS memory
                    let bufferStart = rxblock.bufferStart;
                    if (bufferStart >= 0x10000) bufferStart = (bufferStart & 0xffff) | 0x10000;
 
                    for (let i = 0; i < rxblock.bufferEnd - rxblock.bufferStart; i++) {
                        this.ram[bufferStart + i] = rxblock.data.buffer[i + 4];
                    }
 
                    this.econet.deleteReceiveBlock(this.ram[p]);
                    //console.log("Filestore: read and delete receive block " + this.ram[p]);
                }
                break;
            }
 
            case 0x13:
                this.ram[p + 1] = 254; // Station ID
                break;
 
            case 0x72: {
                // SCSI handling
                let addr =
                    (this.ram[p + 1] | (this.ram[p + 2] << 8) | (this.ram[p + 3] << 16) | (this.ram[p + 4] << 24)) >>>
                    0;
                if (addr >= 0x10000) addr = (addr & 0xffff) | 0x10000; //host memory
 
                let result = 4,
                    sec = ((this.ram[p + 6] & 0x1f) << 16) | (this.ram[p + 7] << 8) | this.ram[p + 8];
                let len = this.ram[p + 9] * 0x100; // SCSI_SECSIZE
 
                if (!len)
                    len =
                        this.ram[p + 11] |
                        ((this.ram[p + 12] << 8) | (this.ram[p + 13] << 16) | ((this.ram[p + 14] << 24) >>> 0));
 
                switch (this.ram[p + 5]) {
                    case 0x08: //read
                    case 0x0a: //write
                        if (this.ram[p + 5] === 0x08) {
                            // read
                            for (let i = sec * 0x100; i < sec * 0x100 + len; i++) {
                                this.ram[addr++] = this.scsi[i];
                            }
                        } // write
                        else
                            for (let i = sec * 0x100; i < sec * 0x100 + len; i++) {
                                this.scsi[i] = this.ram[addr++];
                            }
 
                        result = 0;
                        break;
 
                    default:
                        break;
                }
 
                this.ram[p] = result;
                break;
            }
 
            case 0x73:
                this.ram[p] = 0; //sector
                this.ram[p + 1] = 0;
                this.ram[p + 2] = 0;
                this.ram[p + 3] = 0;
                this.ram[p + 4] = 0;
                break;
 
            default:
                console.log("Filestore: unhandled osword 0x" + this.A.toString(16));
                break;
        }
    }
 
    osbyte() {
        switch (this.A) {
            case 0x0d:
            case 0x0e:
            case 0x0f:
                break;
 
            case 0x32: // Poll transmit block
                if (this.econet.serverTx.bytesInBuffer > 0) {
                    this.X = 0x80;
                } else {
                    this.X = 0;
                }
                break;
 
            case 0x33: {
                // Poll receive block
                let rxblock = this.econet.receiveBlocks.find((e) => e.id === this.X);
                this.X = 0;
                if (rxblock) {
                    this.X = rxblock.controlFlag;
 
                    if (this.X >= 0x80) {
                        this.X = rxblock.controlFlag;
                        //console.log("osbyte found received block id " + rxblock.id + " " + this.X.toString(16) + " " + this.Y.toString(16));
                    }
                }
                break;
            }
 
            case 0x34:
                this.econet.deleteReceiveBlock(this.X);
                break;
            case 0x35:
                break;
            case 0x85:
                this.X = 0;
                this.Y = 32 << 2;
                break;
            case 0x86:
                this.X = this.Y = 0;
                break;
            case 0x87:
            case 0x96:
            case 0x97:
                break;
            case 0xb4:
                this.X = 0;
                break;
            case 0xe5:
                break;
 
            default:
                console.log("Filestore: unhandled osbyte 0x" + this.A.toString(16));
                break;
        }
    }
 
    /** Resolves once the filestore code and its disc are loaded and in place. */
    async reset() {
        console.log("Filestore: initialisation");
 
        const [code, disc] = await Promise.all([loadData("econet/L3FS.dat"), loadData("econet/scsi.dat")]);
        this.l3fs = code;
        this.ram.set(code, 0x400);
        this.PC = 0x400;
        this.SP = 0xff;
        this.A = 1;
        this.scsi = disc;
    }
 
    polltime(cycles) {
        this.pollCount += cycles;
        Eif (this.pollCount > this.emulationSpeed) {
            this.pollCount = 0;
 
            // Decode and execute next instruction
            let op, i, i2, j;
 
            this.XPC = this.PC;
            Iif (this.PC >= 0xf800)
                op = 0x60; //ROM, read rts
            else op = this.GBYTE();
 
            i = op & 0x1f; //row
            i2 = i & 0x03;
            j = op >> 5; //column
 
            if (i === 0 && j < 4) {
                //all implicit except JSR
                switch (j) {
                    case 0: //brk
                        this.PC = this.WORD(0x0202); //BRKV
                        break;
                    case 1: //jsr, absolute
                        this.L = this.GWORD();
                        this.PUSH(--this.PC >> 8);
                        this.PUSH(this.PC);
                        this.PC = this.L;
                        break;
                    case 2: //rti
                        break;
                    case 3: //rts
                        this.PC = this.PULL();
                        this.PC |= this.PULL() << 8;
                        this.PC++;
                        break;
                }
            } else Eif (i === 8) {
                //all implicit
                switch (j) {
                    case 0: //php
                        this.M = (this.N << 7) | (this.V << 6) | (this.Z << 1) | this.C;
                        this.PUSH(this.M);
                        break;
                    case 1: //plp
                        this.M = this.PULL();
                        this.N = (this.M & 0x80) > 0;
                        this.V = (this.M & 0x40) > 0;
                        this.Z = (this.M & 2) > 0;
                        this.C = this.M & 1;
                        break;
                    case 2: //pha
                        this.PUSH(this.A);
                        break;
                    case 3: //pla
                        this.A = this.NZ(this.PULL());
                        break;
                    case 4: //dey
                        this.Y--;
                        this.Y &= 0xff;
                        this.NZ(this.Y);
                        break;
                    case 5: //tay
                        this.Y = this.NZ(this.A);
                        break;
                    case 6: //iny
                        this.Y++;
                        this.Y &= 0xff;
                        this.NZ(this.Y);
                        break;
                    case 7: //inx
                        this.X++;
                        this.X &= 0xff;
                        this.NZ(this.X);
                        break;
                }
            } else if (i === 10 && j > 3) {
                //all implicit
                switch (j) {
                    case 4: //txa
                        this.A = this.NZ(this.X);
                        break;
                    case 5: //tax
                        this.X = this.NZ(this.A);
                        break;
                    case 6: //dex
                        this.X--;
                        this.X &= 0xff;
                        this.NZ(this.X);
                        break;
                    case 7: //nop
                        break;
                }
            } else if (i === 16) {
                //branch, all relative
                let flag = ~j & 1;
                switch (j >> 1) {
                    case 0:
                        flag ^= this.N;
                        break;
                    case 1:
                        flag ^= this.V;
                        break;
                    case 2:
                        flag ^= this.C;
                        break;
                    case 3:
                        flag ^= this.Z;
                        break;
                }
 
                this.M = this.GBYTE();
 
                if (flag) {
                    if (this.M & 0x80) this.PC -= 0x100;
                    this.PC += this.M;
                }
            } else if (i === 24) {
                //all implicit
                switch (j) {
                    case 0: //clc
                        this.C = 0;
                        break;
                    case 1: //sec
                        this.C = 1;
                        break;
                    case 2: //cli
                        break;
                    case 3: //sei
                        break;
                    case 4: //tya
                        this.A = this.NZ(this.Y);
                        break;
                    case 5: //clv
                        this.V = 0;
                        break;
                    case 6: //cld
                        break;
                    case 7: //sed
                        break;
                }
            } else if (i === 26) {
                //all implicit
                if (j & 1)
                    //tsx
                    this.X = this.NZ(this.SP);
                //txs
                else this.SP = this.X;
            } else {
                //multiple address modes
                let acc = 0,
                    imm = 0,
                    store = 0;
 
                if (i === 10) {
                    acc = 1;
                } else {
                    if (i === 1) {
                        //(zp,X)
                        this.L = this.GBYTE() + this.X;
                        this.L = this.WORD(this.L);
                    } else if (i === 25 || op === 190) {
                        //LDX abs,Y
                        this.L = this.GWORD() + this.Y;
                    } else {
                        switch (i >> 2) {
                            case 0: //#
                            case 2: //#
                                imm = 1;
                                break;
                            case 1: //zp
                                this.L = this.GBYTE();
                                break;
                            case 3: //abs
                                this.L = this.GWORD();
                                break;
                            case 4: //(zp),Y
                                this.L = this.GBYTE();
                                this.L = this.WORD(this.L) + this.Y;
                                break;
                            case 5:
                                if (op === 150)
                                    //STX zp,Y
                                    this.L = this.GBYTE() + this.Y;
                                //zp,X
                                else this.L = this.GBYTE() + this.X;
                                break;
                            case 7: //abs,X
                                this.L = this.GWORD() + this.X;
                                break;
                        }
                    }
                }
 
                if (acc) this.M = this.A;
                else if (imm) this.M = this.GBYTE();
                else this.M = this.ram[this.L];
 
                switch (i2) {
                    case 0:
                        switch (j) {
                            case 1: //bit
                                this.NZ(this.A & this.M);
                                this.V = (this.M & 0x40) > 0;
                                this.N = (this.M & 0x80) > 0;
                                break;
                            case 2: //jmp absolute
                                this.PC = this.L;
                                break;
                            case 3: //jmp indirect
                                this.PC = this.WORD(this.L);
                                break;
                            case 4: //sty
                                this.M = this.Y;
                                store = 1;
                                break;
                            case 5: //ldy
                                this.Y = this.NZ(this.M);
                                break;
                            case 6: //cpy
                                this.NZ(this.Y - this.M);
                                this.C = this.Y >= this.M;
                                break;
                            case 7: //cpx
                                this.NZ(this.X - this.M);
                                this.C = this.X >= this.M;
                                break;
                        }
                        break;
                    case 1:
                        switch (j) {
                            case 0: //ora
                                this.A = this.NZ(this.A | this.M);
                                break;
                            case 1: //and
                                this.A = this.NZ(this.A & this.M);
                                break;
                            case 2: //eor
                                this.A = this.NZ(this.A ^ this.M);
                                break;
                            case 7: //sbc
                                this.M = ~this.M & 0xff;
                            // falls through
                            case 3: //adc
                                this.R = this.A + this.M + this.C;
                                this.C = this.R >= 0x100;
                                this.V = ((this.A ^ this.R) & (this.M ^ this.R) & 0x80) > 0;
                                this.A = this.NZ(this.R);
                                break;
                            case 4: //sta
                                this.M = this.A;
                                store = 1;
                                break;
                            case 5: //lda
                                this.A = this.NZ(this.M);
                                break;
                            case 6: //cmp
                                this.NZ(this.A - this.M);
                                this.C = this.A >= this.M;
                                break;
                        }
                        break;
                    case 2:
                        store = 1;
                        switch (j) {
                            case 0: //asl
                                this.C = 0;
                            // falls through
                            case 1: //rol
                                this.F = (this.M & 0x80) > 0;
                                this.M = this.NZ((this.M << 1) | this.C);
                                this.C = this.F;
                                break;
                            case 2: //lsr
                                this.C = 0;
                            // falls through
                            case 3: //ror
                                this.F = this.M & 1;
                                this.M = this.NZ((this.M >> 1) | (this.C << 7));
                                this.C = this.F;
                                break;
                            case 4: //stx
                                this.M = this.X;
                                break;
                            case 5: //ldx
                                this.X = this.NZ(this.M);
                                store = 0;
                                break;
                            case 6: //dec
                                this.M--;
                                this.M &= 0xff;
                                this.NZ(this.M);
                                break;
                            case 7: //inc
                                this.M++;
                                this.M &= 0xff;
                                this.NZ(this.M);
                                break;
                        }
                        break;
                }
 
                if (store) {
                    if (acc) this.A = this.M;
                    else this.ram[this.L] = this.M;
                }
            }
 
            // Have we been asked to execute an OS ROM call?
            Iif (this.PC >= 0xf800) {
                switch (this.PC) {
                    case 0xf800: //reset
                        break;
                    case 0xffe0: //OSRDCH
                        this.A = 83; // The letter 'S' (tells FS3 to start)
                        this.C = 0;
                        break;
                    case 0xfff1: //OSWORD
                        this.osword();
                        break;
                    case 0xfff4: //OSBYTE
                        this.osbyte();
                        break;
                    case 0xffe7: //OSNEWL
                    case 0xffe3: //OSASCI
                    case 0xffee: //OSWRCH
                        this.oswrch(this.A);
                        break;
                    case 0xfff7: //OSCLI
                        break;
                    default:
                        break;
                }
            }
        }
    }
}