aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorUri Shaked2019-11-21 10:59:01 +0200
committerUri Shaked2019-11-21 10:59:34 +0200
commit88674b7a66cfb831d1c50f1970828944246d3259 (patch)
treeaeba6f18b96680b08ceda4ae975dbb73353b3677 /src
parenttest: SWAP, STS (diff)
downloadavr8js-88674b7a66cfb831d1c50f1970828944246d3259.tar.gz
avr8js-88674b7a66cfb831d1c50f1970828944246d3259.tar.bz2
avr8js-88674b7a66cfb831d1c50f1970828944246d3259.zip
feat: implement avrInterrupt()
used to invoke hardware interrupt
Diffstat (limited to 'src')
-rw-r--r--src/index.ts1
-rw-r--r--src/interrupt.spec.ts18
-rw-r--r--src/interrupt.ts11
3 files changed, 30 insertions, 0 deletions
diff --git a/src/index.ts b/src/index.ts
index 038ff16..8a474a4 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,2 +1,3 @@
export { CPU, ICPU, ICPUMemoryHook, ICPUMemoryHooks } from './cpu';
export { avrInstruction } from './instruction';
+export { avrInterrupt } from './interrupt';
diff --git a/src/interrupt.spec.ts b/src/interrupt.spec.ts
new file mode 100644
index 0000000..06979ed
--- /dev/null
+++ b/src/interrupt.spec.ts
@@ -0,0 +1,18 @@
+import { CPU } from './cpu';
+import { avrInterrupt } from './interrupt';
+
+describe('avrInterrupt', () => {
+ it('should execute interrupt handler', () => {
+ const cpu = new CPU(new Uint16Array(0x8000));
+ cpu.pc = 0x520;
+ cpu.data[93] = 0x80; // SP <- 0x80
+ cpu.data[95] = 0b10000001; // SREG <- I------C
+ avrInterrupt(cpu, 5);
+ expect(cpu.cycles).toEqual(2);
+ expect(cpu.pc).toEqual(5);
+ expect(cpu.data[93]).toEqual(0x7e); // SP
+ expect(cpu.data[0x80]).toEqual(0x20); // Return addr low
+ expect(cpu.data[0x7f]).toEqual(0x5); // Return addr high
+ expect(cpu.data[95]).toEqual(0b00000001); // SREG: -------C
+ });
+});
diff --git a/src/interrupt.ts b/src/interrupt.ts
new file mode 100644
index 0000000..d833a3a
--- /dev/null
+++ b/src/interrupt.ts
@@ -0,0 +1,11 @@
+import { ICPU } from './cpu';
+
+export function avrInterrupt(cpu: ICPU, addr: number) {
+ const sp = cpu.dataView.getUint16(93, true);
+ cpu.data[sp] = cpu.pc & 0xff;
+ cpu.data[sp - 1] = (cpu.pc >> 8) & 0xff;
+ cpu.dataView.setUint16(93, sp - 2, true);
+ cpu.data[95] &= 0x7f; // clear global interrupt flag
+ cpu.cycles += 2;
+ cpu.pc = addr;
+}
If you find this content useful please consider a small donation via bitcoin: bitcoin:1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2?amount=0.001&message=Donation Scraping git contents via cgit is very resource intensive. The continued hosting of these git repositories is entirely financed by your bitcoin contributions. For donations of a least 1 mBTC you include your IP address in the message of your bitcoin donation. This will remove this message in the future for requests from your IP, which will significantly reduce token usage. In any case, any contributions to bitcoin address 1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2 are very welcome. Thanks in advance for you contribution to a self-sustaining ecosystem.