aboutsummaryrefslogtreecommitdiff
path: root/src/gpio.spec.ts
diff options
context:
space:
mode:
authorlironh2020-03-21 09:52:53 +0200
committerlironh2020-03-22 21:08:30 +0200
commit8934a7566a038a74464d3d8df9d04fd875e5b1d7 (patch)
treee131c263938081e6c89f39f141f3f20f6da8f851 /src/gpio.spec.ts
parentMerge pull request #19 from gfeun/main-execute-loop-optimization (diff)
downloadavr8js-8934a7566a038a74464d3d8df9d04fd875e5b1d7.tar.gz
avr8js-8934a7566a038a74464d3d8df9d04fd875e5b1d7.tar.bz2
avr8js-8934a7566a038a74464d3d8df9d04fd875e5b1d7.zip
refactor: added peripherals and cpu feature folders
Diffstat (limited to 'src/gpio.spec.ts')
-rw-r--r--src/gpio.spec.ts86
1 files changed, 0 insertions, 86 deletions
diff --git a/src/gpio.spec.ts b/src/gpio.spec.ts
deleted file mode 100644
index 2fa866d..0000000
--- a/src/gpio.spec.ts
+++ /dev/null
@@ -1,86 +0,0 @@
-import { CPU } from './cpu';
-import { AVRIOPort, portBConfig, PinState } from './gpio';
-
-describe('GPIO', () => {
- it('should invoke the listeners when the port is written to', () => {
- const cpu = new CPU(new Uint16Array(1024));
- const port = new AVRIOPort(cpu, portBConfig);
- const listener = jest.fn();
- port.addListener(listener);
- cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f
- cpu.writeData(0x25, 0x55); // PORTB <- 0x55
- expect(listener).toHaveBeenCalledWith(0x05, 0);
- expect(cpu.data[0x23]).toEqual(0x5); // PINB should return port value
- });
-
- it('should toggle the pin when writing to the PIN register', () => {
- const cpu = new CPU(new Uint16Array(1024));
- const port = new AVRIOPort(cpu, portBConfig);
- const listener = jest.fn();
- port.addListener(listener);
- cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f
- cpu.writeData(0x25, 0x55); // PORTB <- 0x55
- cpu.writeData(0x23, 0x01); // PINB <- 0x0f
- expect(listener).toHaveBeenCalledWith(0x04, 0x5);
- expect(cpu.data[0x23]).toEqual(0x4); // PINB should return port value
- });
-
- describe('removeListener', () => {
- it('should remove the given listener', () => {
- const cpu = new CPU(new Uint16Array(1024));
- const port = new AVRIOPort(cpu, portBConfig);
- const listener = jest.fn();
- port.addListener(listener);
- cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f
- port.removeListener(listener);
- cpu.writeData(0x25, 0x99); // PORTB <- 0x99
- expect(listener).not.toHaveBeenCalled();
- });
- });
-
- describe('pinState', () => {
- it('should return PinState.High when the pin set to output and HIGH', () => {
- const cpu = new CPU(new Uint16Array(1024));
- const port = new AVRIOPort(cpu, portBConfig);
- cpu.writeData(0x24, 0x1); // DDRB <- 0x1
- cpu.writeData(0x25, 0x1); // PORTB <- 0x1
- expect(port.pinState(0)).toEqual(PinState.High);
- });
-
- it('should return PinState.Low when the pin set to output and LOW', () => {
- const cpu = new CPU(new Uint16Array(1024));
- const port = new AVRIOPort(cpu, portBConfig);
- cpu.writeData(0x24, 0x8); // DDRB <- 0x8
- cpu.writeData(0x25, 0xf7); // PORTB <- 0xF7 (~8)
- expect(port.pinState(3)).toEqual(PinState.Low);
- });
-
- it('should return PinState.Input by default (reset state)', () => {
- const cpu = new CPU(new Uint16Array(1024));
- const port = new AVRIOPort(cpu, portBConfig);
- expect(port.pinState(1)).toEqual(PinState.Input);
- });
-
- it('should return PinState.InputPullUp when the pin is set to input with pullup', () => {
- const cpu = new CPU(new Uint16Array(1024));
- const port = new AVRIOPort(cpu, portBConfig);
- cpu.writeData(0x24, 0); // DDRB <- 0
- cpu.writeData(0x25, 0x2); // PORTB <- 0x2
- expect(port.pinState(1)).toEqual(PinState.InputPullUp);
- });
-
- it('should reflect the current port state when called inside a listener', () => {
- // Related issue: https://github.com/wokwi/avr8js/issues/9
- const cpu = new CPU(new Uint16Array(1024));
- const port = new AVRIOPort(cpu, portBConfig);
- const listener = jest.fn(() => {
- expect(port.pinState(0)).toBe(PinState.High);
- });
- port.addListener(listener);
- expect(port.pinState(0)).toBe(PinState.Input);
- cpu.writeData(0x24, 0x01); // DDRB <- 0x01
- cpu.writeData(0x25, 0x01); // PORTB <- 0x01
- expect(listener).toHaveBeenCalled();
- });
- });
-});