aboutsummaryrefslogtreecommitdiff
path: root/src/cpu/cpu.spec.ts
blob: 00fbcb3b69b8d17e3c500ca6851e301d80debce7 (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
import { CPU } from './cpu';

describe('cpu', () => {
  it('should set initial value of SP to the last byte of internal SRAM', () => {
    const cpu = new CPU(new Uint16Array(1024), 0x1000);
    expect(cpu.SP).toEqual(0x10ff);
  });

  describe('events', () => {
    it('should execute queued events after the given number of cycles has passed', () => {
      const cpu = new CPU(new Uint16Array(1024), 0x1000);
      const events = [];
      for (const i of [1, 4, 10]) {
        cpu.addClockEvent(() => events.push([i, cpu.cycles]), i);
      }
      for (let i = 0; i < 10; i++) {
        cpu.cycles++;
        cpu.tick();
      }
      expect(events).toEqual([
        [1, 1],
        [4, 4],
        [10, 10],
      ]);
    });

    describe('updateClockEvent', () => {
      it('should update the number of cycles for the given clock event', () => {
        const cpu = new CPU(new Uint16Array(1024), 0x1000);
        const events = [];
        const callbacks = [];
        for (const i of [1, 4, 10]) {
          callbacks[i] = cpu.addClockEvent(() => events.push([i, cpu.cycles]), i);
        }
        cpu.updateClockEvent(callbacks[4], 2);
        cpu.updateClockEvent(callbacks[1], 12);
        for (let i = 0; i < 14; i++) {
          cpu.cycles++;
          cpu.tick();
        }
        expect(events).toEqual([
          [4, 2],
          [10, 10],
          [1, 12],
        ]);
      });

      describe('clearClockEvent', () => {
        it('should remove the given clock event', () => {
          const cpu = new CPU(new Uint16Array(1024), 0x1000);
          const events = [];
          const callbacks = [];
          for (const i of [1, 4, 10]) {
            callbacks[i] = cpu.addClockEvent(() => events.push([i, cpu.cycles]), i);
          }
          cpu.clearClockEvent(callbacks[4]);
          for (let i = 0; i < 10; i++) {
            cpu.cycles++;
            cpu.tick();
          }
          expect(events).toEqual([
            [1, 1],
            [10, 10],
          ]);
        });
      });
    });
  });
});