All files / src cmos.js

86.66% Statements 78/90
82.25% Branches 51/62
100% Functions 10/10
87.05% Lines 74/85

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                  16x           16x     10x 10x 10x       8x       2x                             18x 18x   18x 18x 18x 6x 6x 4x 1x   6x 6x       22x 22x   5x 5x 2x 2x               291x 291x 291x 291x 291x 291x 291x   291x 289x   291x 2x   291x 2x 2x         291x       297x 47x         27x             26x   23x 8x 8x   1x   2x   2x   1x   1x   1x         15x     3x       101x 101x 101x 101x 101x 101x 101x 101x 101x 8x 6x 6x   2x 2x         1x 1x   1x 1x                                       2x 2x          
// CMOS layout: bytes 0-13 are RTC internal registers; bytes 14-63 are the 50
// bytes of user storage.  Storage byte 11 (= CMOS address 25 = 0x19) holds
// the BBC Master DFS configuration byte whose bits [1:0] are the WD1770 step
// rate (*CONFIGURE FDRIVE):  0=6ms  1=12ms  2=20ms  3=30ms.
// The correct default is FDRIVE 0 (6ms, value 0xc8).  The previous default
// 0xca had bits[1:0]=0b10 (20ms), which caused disc-streaming demos such as
// STNICC-beeb to hang on the Master 128 because the WD1770 seek overran the
// vsync window.  Confirmed by @tom-seddon (b2 emulator):
// https://github.com/mattgodbolt/jsbeeb/issues/576
const defaultCmos = Object.freeze([
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0xeb, 0x00,
    0xc9, 0xff, 0xff, 0x12, 0x00, 0x17, 0xc8, 0x1e, 0x05, 0x00, 0x35, 0xa6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);
 
let timeOffset = 0;
 
function getBbcDateTime() {
    const result = new Date(Date.now() + timeOffset);
    result.setMilliseconds(0);
    return result;
}
 
function toBcd(value) {
    return parseInt(value.toString(10), 16);
}
 
function fromBcd(value) {
    return parseInt(value.toString(16), 10);
}
 
export { defaultCmos };
 
/**
 * CMOS persistence backed by a browser storage object, which can be unavailable, full or holding
 * something other than what was last saved.
 *
 * @param {function(): Storage} getStorage typically `() => window.localStorage`. Reading that
 *   property is itself what throws when a page is refused storage, so it happens per call, inside
 *   the try.
 * @param {function(*)} onSaveFailure called with the error the first time a save fails
 */
export function localStoragePersistence(getStorage, onSaveFailure) {
    let saveFailureReported = false;
    return {
        load() {
            try {
                const stored = getStorage().cmosRam;
                if (!stored) return null;
                const parsed = JSON.parse(stored);
                if (!Array.isArray(parsed) || parsed.length !== defaultCmos.length)
                    throw new Error(`the stored settings are not ${defaultCmos.length} bytes`);
                return parsed;
            } catch (error) {
                console.log(`Unable to read the stored CMOS settings: ${error?.message ?? error}`);
                return null;
            }
        },
        save(data) {
            try {
                getStorage().cmosRam = JSON.stringify(data);
            } catch (error) {
                console.log(`Unable to store the CMOS settings: ${error?.message ?? error}`);
                if (saveFailureReported) return;
                saveFailureReported = true;
                onSaveFailure(error);
            }
        },
    };
}
 
export class Cmos {
    constructor(persistence, cmosOverride, econet) {
        this.store = persistence ? persistence.load() : null;
        this.persistence = persistence;
        this.enabled = false;
        this.isRead = false;
        this.addressSelect = false;
        this.dataSelect = false;
        this.cmosAddr = 0;
 
        if (!this.store) {
            this.store = [...defaultCmos];
        }
        if (cmosOverride) {
            this.store = cmosOverride(this.store);
        }
        if (econet) {
            this.store[0xe] = econet.stationId;
            Iif (this.store[0xf] === 0) {
                // Catch an invalid FS configuration setting (possibly as a result of using a prior version that didn't set a default of 254 in CMOS)
                this.store[0xf] = 254;
            }
        }
        this.save();
    }
 
    save() {
        if (this.persistence) {
            this.persistence.save(this.store);
        }
    }
 
    read() {
        if (!this.enabled) return 0xff;
        // To drive the bus we need:
        // - CMOS enabled.
        // - Address Select low.
        // - Data Select high.
        // - Read high.
 
        if (!this.addressSelect && this.dataSelect && this.isRead) {
            // The first 10 bytes of CMOS RAM store the RTC clock
            if (this.cmosAddr < 10) {
                const current = getBbcDateTime();
                switch (this.cmosAddr) {
                    case 0:
                        return toBcd(current.getSeconds());
                    case 2:
                        return toBcd(current.getMinutes());
                    case 4:
                        return toBcd(current.getHours());
                    case 6:
                        return toBcd(current.getDay() + 1);
                    case 7:
                        return toBcd(current.getDate());
                    case 8:
                        return toBcd(current.getMonth() + 1);
                    case 9:
                        return toBcd(current.getFullYear());
                }
            } else {
                return this.store[this.cmosAddr] & 0xff;
            }
        }
        return 0xff;
    }
 
    writeControl(portBpins, portApins, IC32) {
        this.enabled = !!(portBpins & 0x40);
        Iif (!this.enabled) return;
        const oldDataSelect = this.dataSelect;
        const oldAddressSelect = this.addressSelect;
        this.isRead = !!(IC32 & 2);
        this.dataSelect = !!(IC32 & 4);
        this.addressSelect = !!(portBpins & 0x80);
        if (oldAddressSelect && !this.addressSelect) this.cmosAddr = portApins & 0x3f;
        if (oldDataSelect && !this.dataSelect && !this.addressSelect && !this.isRead) {
            if (this.cmosAddr > 0xb) {
                this.store[this.cmosAddr] = portApins;
                this.save();
            } else {
                const bbcTime = getBbcDateTime();
                switch (this.cmosAddr) {
                    case 0:
                        bbcTime.setSeconds(fromBcd(portApins));
                        break;
                    case 2:
                        bbcTime.setMinutes(fromBcd(portApins));
                        break;
                    case 4:
                        bbcTime.setHours(fromBcd(portApins));
                        break;
                    // I tried some day offset stuff to lazily simulate the fact the day is separate
                    // from the date, but I couldn't easily get it to work during the multi-write update
                    // cycle/ I'm probably being dumb. Or else I should emulate the clock "properly" but
                    // that seems like an awful lot of work.
                    // case 6:
                    //     dayOffset = (bbcTime.getDay() - fromBcd(portApins - 1)) % 7;
                    //     break;
                    case 7:
                        bbcTime.setDate(fromBcd(portApins));
                        break;
                    case 8:
                        bbcTime.setMonth(fromBcd(portApins - 1));
                        break;
                    case 9: {
                        const yearBase = fromBcd(portApins) > 80 ? 1900 : 2000;
                        bbcTime.setFullYear(fromBcd(portApins) + yearBase);
                        break;
                    }
                }
                const secondsNow = Math.floor(Date.now() / 1000) * 1000;
                timeOffset = bbcTime.getTime() - secondsNow;
            }
        }
    }
}