|
| 1 | +import { CPU } from '../cpu/cpu'; |
| 2 | +import { AVRClock, clockConfig } from './clock'; |
| 3 | + |
| 4 | +// Clock Registers |
| 5 | +const CLKPC = 0x61; |
| 6 | + |
| 7 | +// Register bit names |
| 8 | +const CLKPCE = 128; |
| 9 | + |
| 10 | +describe('Clock', () => { |
| 11 | + it('should set the prescaler when double-writing CLKPC', () => { |
| 12 | + const cpu = new CPU(new Uint16Array(0x1000)); |
| 13 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 14 | + cpu.writeData(CLKPC, CLKPCE); |
| 15 | + cpu.writeData(CLKPC, 3); // Divide by 8 (2^3) |
| 16 | + expect(clock.frequency).toEqual(2e6); // 2MHz |
| 17 | + expect(cpu.readData(CLKPC)).toEqual(3); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should not update the prescaler if CLKPCE was not set CLKPC', () => { |
| 21 | + const cpu = new CPU(new Uint16Array(0x1000)); |
| 22 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 23 | + cpu.writeData(CLKPC, 3); // Divide by 8 (2^3) |
| 24 | + expect(clock.frequency).toEqual(16e6); // still 16MHz |
| 25 | + expect(cpu.readData(CLKPC)).toEqual(0); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should not update the prescaler if more than 4 cycles passed since setting CLKPCE', () => { |
| 29 | + const cpu = new CPU(new Uint16Array(0x1000)); |
| 30 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 31 | + cpu.writeData(CLKPC, CLKPCE); |
| 32 | + cpu.cycles += 6; |
| 33 | + cpu.writeData(CLKPC, 3); // Divide by 8 (2^3) |
| 34 | + expect(clock.frequency).toEqual(16e6); // still 16MHz |
| 35 | + expect(cpu.readData(CLKPC)).toEqual(0); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('prescaler property', () => { |
| 39 | + it('should return the current prescaler value', () => { |
| 40 | + const cpu = new CPU(new Uint16Array(0x1000)); |
| 41 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 42 | + cpu.writeData(CLKPC, CLKPCE); |
| 43 | + cpu.writeData(CLKPC, 5); // Divide by 32 (2^5) |
| 44 | + cpu.cycles = 16e6; |
| 45 | + expect(clock.prescaler).toEqual(32); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('time properties', () => { |
| 50 | + it('should return current number of microseconds, derived from base freq + prescaler', () => { |
| 51 | + const cpu = new CPU(new Uint16Array(0x1000)); |
| 52 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 53 | + cpu.writeData(CLKPC, CLKPCE); |
| 54 | + cpu.writeData(CLKPC, 2); // Divide by 4 (2^2) |
| 55 | + cpu.cycles = 16e6; |
| 56 | + expect(clock.timeMillis).toEqual(4000); // 4 seconds |
| 57 | + }); |
| 58 | + |
| 59 | + it('should return current number of milliseconds, derived from base freq + prescaler', () => { |
| 60 | + const cpu = new CPU(new Uint16Array(0x1000)); |
| 61 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 62 | + cpu.writeData(CLKPC, CLKPCE); |
| 63 | + cpu.writeData(CLKPC, 2); // Divide by 4 (2^2) |
| 64 | + cpu.cycles = 16e6; |
| 65 | + expect(clock.timeMicros).toEqual(4e6); // 4 seconds |
| 66 | + }); |
| 67 | + |
| 68 | + it('should return current number of nanoseconds, derived from base freq + prescaler', () => { |
| 69 | + const cpu = new CPU(new Uint16Array(0x1000)); |
| 70 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 71 | + cpu.writeData(CLKPC, CLKPCE); |
| 72 | + cpu.writeData(CLKPC, 2); // Divide by 4 (2^2) |
| 73 | + cpu.cycles = 16e6; |
| 74 | + expect(clock.timeNanos).toEqual(4e9); // 4 seconds |
| 75 | + }); |
| 76 | + |
| 77 | + it('should correctly calculate time when changing the prescale value at runtime', () => { |
| 78 | + const cpu = new CPU(new Uint16Array(0x1000)); |
| 79 | + const clock = new AVRClock(cpu, 16e6, clockConfig); |
| 80 | + cpu.cycles = 16e6; // run 1 second at 16MHz |
| 81 | + cpu.writeData(CLKPC, CLKPCE); |
| 82 | + cpu.writeData(CLKPC, 2); // Divide by 4 (2^2) |
| 83 | + cpu.cycles += 2 * 4e6; // run 2 more seconds at 4MhZ |
| 84 | + expect(clock.timeMillis).toEqual(3000); // 3 seconds in total |
| 85 | + |
| 86 | + cpu.writeData(CLKPC, CLKPCE); |
| 87 | + cpu.writeData(CLKPC, 1); // Divide by 2 (2^1) |
| 88 | + cpu.cycles += 0.5 * 8e6; // run 0.5 more seconds at 8MhZ |
| 89 | + expect(clock.timeMillis).toEqual(3500); // 3.5 seconds in total |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments