aboutsummaryrefslogtreecommitdiffgithub
path: root/novice/simulator/simulator.ts
blob: 4800e4b9a32036e133652ac76ca9be4936e308bb (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
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
import { Fields, InstructionSpec, IO, Isa, MachineCodeSection, MachineStateLogEntry,
         MachineStateUpdate, Reg, RegIdentifier } from '../isa';
import { forceUnsigned, maskTo, sextTo } from '../util';
import { Memory } from './mem';

class InstrLut {
    private isa: Isa;
    private lookupBits: number;
    // spec, mask, val, #bits
    private lut: [InstructionSpec, number, number, number][][];

    public constructor(isa: Isa, lookupBits: number) {
        this.isa = isa;
        this.lookupBits = Math.min(isa.pc.instrBits, lookupBits);
        this.lut = this.genLut(lookupBits);
    }

    public lookup(ir: number): [InstructionSpec, number, number, number][] {
        const idx = maskTo(ir >> (this.isa.pc.instrBits - this.lookupBits),
                           this.lookupBits);

        return this.lut[idx];
    }

    private genLut(lookupBits: number) {
        const lut: [InstructionSpec, number, number, number][][] = [];

        for (let i = 0; i < Math.pow(2, lookupBits); i++) {
            lut.push([]);
        }

        for (const instr of this.isa.instructions) {
            let mask = 0;
            let val = 0;
            let totalBits = 0;
            let firstNBits = [0];

            // Sort them so fields are in the right order
            const fields = instr.fields.slice(0).sort(
                (left, right) => right.bits[0] - left.bits[0]);

            for (const field of fields) {
                const numBits = field.bits[0] - field.bits[1] + 1;
                const needBits = this.lookupBits - (this.isa.pc.instrBits - field.bits[0] - 1);
                // Take the most significant X bits from this field
                const whichBits = Math.min(numBits, needBits);

                if (field.kind === 'const') {
                    if (whichBits > 0) {
                        // thinkin bout thos bits
                        const thosBits = field.val >> (numBits - whichBits);
                        firstNBits = firstNBits.map(bits => (bits << whichBits) | thosBits);
                    }

                    const babymask = maskTo(-1, numBits);
                    mask |= babymask << field.bits[1];
                    val |= (field.val & babymask) << field.bits[1];
                    totalBits += numBits;
                } else if (whichBits > 0) {
                    const newFirstNBits: number[] = [];

                    // In this case, we need to add all 2^whichBits
                    // combinations
                    for (let i = 0; i < Math.pow(2, whichBits); i++) {
                        for (const bits of firstNBits) {
                            newFirstNBits.push((bits << whichBits) | i);
                        }
                    }

                    firstNBits = newFirstNBits;
                }
            }

            const entry: [InstructionSpec, number, number, number] =
                [instr, mask, val, totalBits];
            for (const bits of firstNBits) {
                lut[bits].push(entry);
            }
        }

        return lut;
    }
}

class Simulator implements Memory {
    protected pc: number;
    protected mem: {[addr: number]: number};
    protected regs: {
        solo: {[name: string]: number};
        range: {[prefix: string]: number[]};
    };
    protected halted: boolean;
    protected isa: Isa;
    protected io: IO;
    protected maxExec: number;
    protected log: MachineStateLogEntry[];
    protected numExec: number;
    protected lut: InstrLut;

    public constructor(isa: Isa, io: IO, maxExec: number) {
        // 64 entries is a nice cozy size without being too gigantic
        const LUT_SIZE = 6;

        this.isa = isa;
        this.io = io;
        this.maxExec = maxExec;
        this.pc = isa.pc.resetVector;
        this.mem = {};
        this.regs = {
            solo: {},
            range: {},
        };
        this.halted = false;
        this.log = [];
        this.numExec = 0;
        this.lut = new InstrLut(this.isa, LUT_SIZE);

        this.resetRegs();
    }

    public getPc(): number { return this.pc; }

    public isHalted(): boolean { return this.halted; }

    // TODO: make this immutable somehow
    public getRegs() { return this.regs; }

    public loadSections(sections: MachineCodeSection[]): void {
        for (const section of sections) {
            for (let i = 0; i < section.words.length; i++) {
                this.store(section.startAddr + i, section.words[i]);
            }
        }
    }

    public async step(): Promise<void> {
        // If already halted, do nothing
        if (this.halted) {
            return;
        }

        this.numExec++;

        const ir = this.load(this.pc);
        this.pc += this.isa.pc.increment;

        const [instr, fields] = this.decode(ir);
        // Don't pass the incremented PC
        const state = {pc: this.pc - this.isa.pc.increment,
                       reg: this.reg.bind(this),
                       load: this.load.bind(this)};
        const ret = instr.sim(state, this.io, fields);
        const updates: MachineStateUpdate[] =
            (Promise.resolve(ret) === ret)
            ? await (ret as Promise<MachineStateUpdate[]>)
            : ret as MachineStateUpdate[];
        const logEntry: MachineStateLogEntry = {instr, fields, deltas: []};

        for (const update of updates) {
            const delta = {update, old: 0};

            switch (update.kind) {
                case 'reg':
                    delta.old = this.reg(update.reg);
                    break;

                case 'mem':
                    delta.old = this.load(update.addr);
                    break;

                case 'pc':
                    delta.old = this.pc;
                    break;

                case 'halt':
                    // Nothing to back up
                    break;

                default:
                    const _: never = update;
            }

            logEntry.deltas.push(delta);
        }

        this.pushLogEntry(logEntry);
    }

    public async run(): Promise<void> {
        while (!this.halted) {
            if (this.maxExec >= 0 && this.numExec >= this.maxExec) {
                throw new Error(`hit maximum executed instruction count ` +
                                `${this.maxExec}. this may indicate an ` +
                                `infinite loop in code`);
            }

            await this.step();
        }
    }

    public pushLogEntry(logEntry: MachineStateLogEntry) {
        for (const delta of logEntry.deltas) {
            const update = delta.update;
            switch (update.kind) {
                case 'reg':
                    this.regSet(update.reg, update.val);
                    break;

                case 'mem':
                    this.store(update.addr, update.val);
                    break;

                case 'pc':
                    this.pc = update.where;
                    break;

                case 'halt':
                    this.halted = true;
                    break;

                default:
                    const _: never = update;
            }
        }

        this.log.push(logEntry);
    }

    public rewind(): void {
        while (this.log.length) {
            this.unstep();
        }
    }

    public unstep(): void {
        this.popLogEntry();
        this.pc -= this.isa.pc.increment;
    }

    public popLogEntry(): MachineStateLogEntry {
        const logEntry = this.log.pop();

        if (!logEntry) {
            throw new Error('already at the beginning of time');
        }

        for (let i = logEntry.deltas.length - 1; i >= 0; i--) {
            const delta = logEntry.deltas[i];
            const update = delta.update;
            switch (update.kind) {
                case 'reg':
                    this.regSet(update.reg, delta.old);
                    break;

                case 'mem':
                    this.store(update.addr, delta.old);
                    break;

                case 'pc':
                    this.pc = delta.old;
                    break;

                case 'halt':
                    this.halted = false;
                    break;

                default:
                    const _: never = update;
            }
        }

        return logEntry;
    }

    public load(addr: number): number {
        if (this.mem.hasOwnProperty(addr)) {
            return this.mem[addr];
        } else {
            return 0;
        }
    }

    public store(addr: number, val: number): void {
        this.mem[addr] = maskTo(val, this.isa.mem.word);
    }

    public reg(id: RegIdentifier): number {
        const reg = this.lookupRegSpec(id);
        let val;

        if (typeof id === 'string') {
            val = this.regs.solo[id];
        } else {
            val = this.regs.range[id[0]][id[1]];
        }

        if (reg.sext) {
            val = sextTo(val, reg.bits);
        }

        return val;
    }

    public regSet(id: RegIdentifier, val: number) {
        const reg = this.lookupRegSpec(id);
        val = maskTo(val, reg.bits);

        if (typeof id === 'string') {
            this.regs.solo[id] = val;
        } else {
            this.regs.range[id[0]][id[1]] = val;
        }
    }

    public decode(ir: number): [InstructionSpec, Fields] {
        const matches = [];
        const candidates = this.lut.lookup(ir);

        for (const instrTuple of candidates) {
            const [instr, mask, val, bits] = instrTuple;
            if ((ir & mask) === val) {
                matches.push({bits, instr});
            }
        }

        if (!matches.length) {
            const unsigned = forceUnsigned(ir, this.isa.pc.instrBits);
            throw new Error(`cannot decode instruction ` +
                            `0x${unsigned.toString(16)}`);
        }

        matches.sort((left, right) => right.bits - left.bits);

        const instruction = matches[0].instr;
        return [instruction, this.genFields(ir, instruction)];
    }

    private lookupRegSpec(id: RegIdentifier): Reg {
        for (const reg of this.isa.regs) {
            if (typeof id === 'string' && reg.kind === 'reg'
                    && reg.name === id ||
                typeof id !== 'string' && reg.kind === 'reg-range'
                    && id[0] === reg.prefix) {
                return reg;
            }
        }

        throw new Error(`unknown register identifier ${id}`);
    }

    private genFields(ir: number, instr: InstructionSpec): Fields {
        const fields: Fields = {regs: {}, imms: {}};

        for (const field of instr.fields) {
            if (field.kind === 'const') {
                continue;
            }

            const numBits = field.bits[0] - field.bits[1] + 1;
            let val = maskTo(ir >> field.bits[1], numBits);

            if (field.kind === 'reg') {
                fields.regs[field.name] = [field.prefix, val];
            } else if (field.kind === 'imm') {
                if (field.sext) {
                    val = sextTo(val, numBits);
                }
                fields.imms[field.name] = val;
            }
        }

        return fields;
    }

    private resetRegs(): void {
        for (const reg of this.isa.regs) {
            switch (reg.kind) {
                case 'reg':
                    this.regs.solo[reg.name] = 0;
                    break;

                case 'reg-range':
                    this.regs.range[reg.prefix] = new Array<number>(reg.count);
                    for (let i = 0; i < reg.count; i++) {
                        this.regs.range[reg.prefix][i] = 0;
                    }
                    break;
            }
        }
    }
}

export { Simulator };