{"id":410,"date":"2010-09-29T14:47:12","date_gmt":"2010-09-29T22:47:12","guid":{"rendered":"http:\/\/www.pagetable.com\/?p=410"},"modified":"2010-09-29T14:47:12","modified_gmt":"2010-09-29T22:47:12","slug":"internals-of-brkirqnmireset-on-a-mos-6502","status":"publish","type":"post","link":"https:\/\/www.pagetable.com\/?p=410","title":{"rendered":"Internals of BRK\/IRQ\/NMI\/RESET on a MOS 6502"},"content":{"rendered":"<p>After 35 years of measuring the behaviour of the MOS 6502 CPU to better understand what is going on, the <a href=\"http:\/\/visual6502.org\/JSSim\/index.html\">Visual6502<\/a> simulator finally allows us insight into the chip, so we can understand what the CPU does internally. One interesting thing here is the question how the 6502 handles BRK, IRQ, NMI and RESET.<\/p>\n<h2>The Specification<\/h2>\n<p>Let&#8217;s revisit the documented part first. The 6502 knows three vectors at the top of its address space:<\/p>\n<table border=\"1\">\n<tr>\n<th>Signal<\/th>\n<th>Vector<\/th>\n<\/tr>\n<tr>\n<td>NMI<\/td>\n<td>$FFFA\/$FFFB<\/td>\n<\/tr>\n<tr>\n<td>RESET<\/td>\n<td>$FFFC\/$FFFD<\/td>\n<\/tr>\n<tr>\n<td>IRQ\/BRK<\/td>\n<td>$FFFE\/$FFFF<\/td>\n<\/tr>\n<\/table>\n<ul>\n<li>On a RESET, the CPU loads the vector from $FFFC\/$FFFD into the program counter and continues fetching instructions from there.<\/li>\n<li>On an NMI, the CPU pushes the low byte and the high byte of the program counter as well as the processor status onto the stack, disables interrupts and loads the vector from $FFFA\/$FFFB into the program counter and continues fetching instructions from there. <\/li>\n<li>On an IRQ, the CPU does the same as in the NMI case, but uses the vector at $FFFE\/$FFFF.<\/li>\n<li>On a BRK instruction, the CPU does the same as in the IRQ case, but sets bit #4 (B flag) in the copy of the status register that is saved on the stack.<\/li>\n<\/ul>\n<p>The four operations are very similar, they only differ in the location of the vector, whether they actually push data onto the stack, and whether they set the B flag.<\/p>\n<table border=\"1\">\n<tr>\n<th>Signal<\/th>\n<th>Vector<\/th>\n<th>Push PC and P<\/th>\n<th>Set B Flag<\/th>\n<\/tr>\n<tr>\n<td>NMI<\/td>\n<td>$FFFA\/$FFFB<\/td>\n<td align=\"center\">yes<\/td>\n<td align=\"center\">no<\/td>\n<\/tr>\n<tr>\n<td>RESET<\/td>\n<td>$FFFC\/$FFFD<\/td>\n<td align=\"center\">no<\/td>\n<td align=\"center\">no<\/td>\n<\/tr>\n<tr>\n<td>IRQ<\/td>\n<td>$FFFE\/$FFFF<\/td>\n<td align=\"center\">yes<\/td>\n<td align=\"center\">no<\/td>\n<\/tr>\n<td>BRK<\/td>\n<td>$FFFE\/$FFFF<\/td>\n<td align=\"center\">yes<\/td>\n<td align=\"center\">yes<\/td>\n<\/tr>\n<\/table>\n<h2>BRK<\/h2>\n<p>Ignoring opcode fetches, the <a href=\"http:\/\/www.pagetable.com\/?p=39\">PLA ROM<\/a> defines the following cycles of the BRK instruction (<a href=\"http:\/\/archive.6502.org\/datasheets\/synertek_programming_manual.pdf\">6502 Programming Manual<\/a>, page 131):<\/p>\n<ul>\n<li>store PC(hi)<\/li>\n<li>store PC(lo)<\/li>\n<li>store P<\/li>\n<li>fetch PC(lo) from $FFFE<\/li>\n<li>fetch PC(hi) from $FFFF<\/li>\n<\/ul>\n<h2>IRQ<\/h2>\n<p>An IRQ does basically the same thing as a BRK, but it clears the B flag in the pushed status byte. The CPU goes through the same sequence of cycles as in the BRK case, which is done like this:<\/p>\n<p>If there is an IRQ pending and the current instruction has just finished, the interrupt logic in the 6502 forces the instruction register (&#8220;IR&#8221;) to &#8220;0&#8221;, so instead of executing the next instruction, the PLA will decode the instruction with the opcode 0x00 &#8211; which is BRK! Of course it has to kick in a few cycles later again to make sure a B value of 0 is pushed, but otherwise, it&#8217;s just the BRK instruction executing.<\/p>\n<h2>NMI<\/h2>\n<p>Not surprisingly, NMI is done the same way: &#8220;0&#8221; is injected into the instruction stream, but this time, some extra logic makes sure that the addresses $FFFA\/$FFFB are put onto the address bus when fetching the vector.<\/p>\n<h2>RESET<\/h2>\n<p>RESET also runs through the same sequence, but it is the most different of the four cases, since it does not write the current PC and status onto the stack &#8211; but this was hacked trivially: The bus cycles exist, but the read\/write line is not set to &#8220;write&#8221;, but &#8220;read&#8221; instead. The following trace was created with the transistor data from the Visual 6502 project and shows the first nine cycles after letting go of RESET:<\/p>\n<pre>\n#0 AB:00FF D:00 R\/W:1 PC:00FF A:AA X:00 Y:00 SP:00 P:02 IR:00  READ $00FF = $00\n<\/pre>\n<p><b>Cycle 0<\/b>: When a 6502 is turned on, the stack pointer is initialized with zero. The BRK\/IRQ\/NMI\/RESET sequence pulls the instruction register (IR) to 0.<\/p>\n<pre>\n#1 AB:00FF D:00 R\/W:1 PC:00FF A:AA X:00 Y:00 SP:00 P:02 IR:00  READ $00FF = $00\n#2 AB:00FF D:00 R\/W:1 PC:00FF A:AA X:00 Y:00 SP:00 P:02 IR:00  READ $00FF = $00\n#3 AB:0100 D:00 R\/W:1 PC:00FF A:AA X:00 Y:00 SP:00 P:02 IR:00  READ $0100 = $00\n<\/pre>\n<p><b>Cycle 3<\/b>: The first stack access happens at address $0100 &#8211; a push first stores the value at $0100 + SP, then decrements SP. In the BRK\/IRQ\/NMI case, this would have stored the high-byte of the PC. But for RESET, it is a read cycle, not a write cycle, and the result is discarded.<\/p>\n<pre>\n#4 AB:01FF D:00 R\/W:1 PC:00FF A:AA X:00 Y:00 SP:00 P:02 IR:00  READ $01FF = $00\n<\/pre>\n<p><b>Cycle 4<\/b>: SP is now 0xFF (even if the internal state does not reflect that), so the second stack access (which would have been the low-byte of PC) targets 0x01FF. Again, the result is discarded, and SP decremented.<\/p>\n<pre>\n#5 AB:01FE D:00 R\/W:1 PC:00FF A:AA X:00 Y:00 SP:00 P:02 IR:00  READ $01FE = $00\n<\/pre>\n<p><b>Cycle 5<\/b>: SP is now 0xFE, and the third stack access, (the status register) happens at 0x01FE. SP is decremented again.<\/p>\n<pre>\n#6 AB:FFFC D:E2 R\/W:1 PC:00FF A:AA X:00 Y:00 SP:FD P:06 IR:00  READ $FFFC = $E2\n<\/pre>\n<p><b>Cycle 6<\/b>: The internal state of the CPU now shows that SP is 0xFD, because it got decremented 3 times for the three fake push operations. The low-byte of the vector is read.<\/p>\n<pre>\n#7 AB:FFFD D:FC R\/W:1 PC:00FF A:AA X:00 Y:00 SP:FD P:16 IR:00  READ $FFFD = $FC\n<\/pre>\n<p><b>Cycle 7<\/b>: The high-byte of the vector is read.<\/p>\n<pre>\n#8 AB:FCE2 D:A2 R\/W:1 PC:FCE2 A:AA X:00 Y:00 SP:FD P:16 IR:00  READ $FCE2 = $A2\n<\/pre>\n<p><b>Cycle 8<\/b>: The first actual instruction is fetched.<\/p>\n<p>Since RESET is not timing critical, it doesn&#8217;t matter whether a few cycles are wasted by doing the fake stack cycles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After 35 years of measuring the behaviour of the MOS 6502 CPU to better understand what is going on, the Visual6502 simulator finally allows us insight into the chip, so we can understand what the CPU does internally. One interesting thing here is the question how the 6502 handles BRK, IRQ, NMI and RESET. The &#8230; <a title=\"Internals of BRK\/IRQ\/NMI\/RESET on a MOS 6502\" class=\"read-more\" href=\"https:\/\/www.pagetable.com\/?p=410\" aria-label=\"Read more about Internals of BRK\/IRQ\/NMI\/RESET on a MOS 6502\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5,17,33],"tags":[],"class_list":["post-410","post","type-post","status-publish","format-standard","hentry","category-2","category-archeology","category-hacks","category-trivia"],"_links":{"self":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=410"}],"version-history":[{"count":0,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/410\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}