interupts
interupts
I am currently in the process of designing a microprocessor. The only thing I cannot figure out is how to implement interrupts. Can anyone give me a schematic or a very detailed description on how I can add interrupts to my microprocessor? I want it to be based of the 6502.
Re: interupts
Welcome Gadersd! Do you intend to build actual hardware or are you creating simulation software that'll run on a host CPU?
Interrupts are less complex than you might suppose. If you already understand how a 6502 fetches and executes instructions from memory, that covers a lot of what you need to know. Executing an interrupt is simply a variation on the process of executing an instruction fetched from memory. In fact, it may help to think of an interrupt as an instruction not fetched from memory. It is triggered by the IRQ or NMI input instead. (RESET operates similarly.)
The normal fetch-execute process for 6502 is basically this:
The point about not incrementing the Program Counter is simple when you think about it. Since an op-code was fetched but not executed (being pre-empted by BRK), it's necessary that, after the interrupt and its service routine are complete, the opcode be fetched again for another attempt. In order to fetch that same op-code again, the PC (which gets pushed to stack by BRK then restored upon RTI) must be the same -- ie, not incremented.
Re: almost* -- The subtle reality is that many instructions are able to perform their final cycle internally (without using the bus). Exploiting that fact, 65xx CPU's save a cycle by allowing the op-code fetch to occur simultaneously. The internal behavior is hidden, and generally of no relevance -- a curiosity.
It may seem wasteful that an interrupt causes an opcode to be fetched and then discarded. But the internal operation just described necessitates a one-cycle delay anyway, so the wasted fetch is of no consequence.
HTH! Cheers,
Jeff
Interrupts are less complex than you might suppose. If you already understand how a 6502 fetches and executes instructions from memory, that covers a lot of what you need to know. Executing an interrupt is simply a variation on the process of executing an instruction fetched from memory. In fact, it may help to think of an interrupt as an instruction not fetched from memory. It is triggered by the IRQ or NMI input instead. (RESET operates similarly.)
The normal fetch-execute process for 6502 is basically this:
- wait until the currently-executing instruction is (almost*) complete.
- Use the Program Counter to drive the address bus and thus fetch an op-code from memory. The SYNC (or, for 65816, VPA and VDA) output(s) will be high.
- save the op-code internally on-chip
- increment the Program Counter
- perform whatever specific actions are required by the op-code
- wait until the currently-executing instruction is (almost*) complete.
- use the Program Counter to drive the address bus and thus fetch an op-code from memory. The output(s) will be high, even though the opcode won't execute.
- discard the op-code that was fetched. Instead, save a BRK op-code ($00) internally on-chip
- do not increment the Program Counter
- perform whatever specific actions are required by the op-code (in this case, BRK)
The point about not incrementing the Program Counter is simple when you think about it. Since an op-code was fetched but not executed (being pre-empted by BRK), it's necessary that, after the interrupt and its service routine are complete, the opcode be fetched again for another attempt. In order to fetch that same op-code again, the PC (which gets pushed to stack by BRK then restored upon RTI) must be the same -- ie, not incremented.
Re: almost* -- The subtle reality is that many instructions are able to perform their final cycle internally (without using the bus). Exploiting that fact, 65xx CPU's save a cycle by allowing the op-code fetch to occur simultaneously. The internal behavior is hidden, and generally of no relevance -- a curiosity.
It may seem wasteful that an interrupt causes an opcode to be fetched and then discarded. But the internal operation just described necessitates a one-cycle delay anyway, so the wasted fetch is of no consequence.
HTH! Cheers,
Jeff
Last edited by Dr Jefyll on Wed Jun 12, 2013 2:46 am, edited 2 times in total.
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
https://laughtonelectronics.com/Arcana/ ... mmary.html
Re: interupts
Thanks! Your answer was very helpful. I am planning on eventually building the hardware from ics.
Re: interupts
I just read that the brk instruction loads the PC with the address at FFFF and FFFE. How would this work when the NMI and RESET are located at different addresses?
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: interupts
Gadersd wrote:
I just read that the brk instruction loads the PC with the address at FFFF and FFFE. How would this work when the NMI and RESET are located at different addresses?
Make sure you've read the 6502 interrupts primer.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: interupts
I don't know how the 6502 actually does this, but if I was designing it, I'd have the logic that generates the vector address look at /NMI and /RESET. You could almost just feed those two inputs directly to the appropriate address lines (A2 and A1 respectively), except you have to handle the case where both are asserted at the same time (/RESET has priority). And it'd probably be best to store both inputs at the moment the opcode is fetched, just in case they change later (it would be very bad if one changed half way through fetching the vector).
I never thought of it before, but it's likely that this is why BRK is $00 on the 6502. In NMOS, overriding a signal with a 0 takes just one transistor.
I never thought of it before, but it's likely that this is why BRK is $00 on the 6502. In NMOS, overriding a signal with a 0 takes just one transistor.
Re: interupts
In my 6502 core, all of RESET/NMI/IRQ are implemented by faking a BRK instruction, and then setting the program counter to the appropriate vector.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: interupts
Quote:
I never thought of it before, but it's likely that this is why BRK is $00 on the 6502.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
- BigDumbDinosaur
- Posts: 9425
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: interupts
GARTHWILSON wrote:
...This is why BRK is a two-byte instruction.
As is COP on the 65C816. The assembly language syntax for COP requires that a signature byte be present, which makes it different from BRK in that respect—also, COP has its own vector.
Now for something interesting: if one studies the instruction execution sequences of BRK and COP, it is seen that during cycle 2 reference is made to the signature byte—VPA is valid at that time, indicating that a fetch from program code is in progress. The signature is in fact loaded by the '816 but doesn't show up in a register. How cool would it be if the signature could have somehow been made to be accessible in the MPU instead of by getting the RTI address off the stack and decrementing it to get to the signature?
x86? We ain't got no x86. We don't NEED no stinking x86!