How does a 6502 processor know when to stop F-D-E-U cycle?

Building your first 6502-based project? We'll help you get started here.
Post Reply
randomkiwi
Posts: 5
Joined: 26 Oct 2023

How does a 6502 processor know when to stop F-D-E-U cycle?

Post by randomkiwi »

I was wondering how does a 6502 processor knows when to stop the Fetch-Decode-Execute-Update cycle
Suppose I have a few lines of instruction say:
LDA $01
ADC $02
STA $0671
I get the program execution up to the first three instruction, but how does the processor knows after executing the third instruction here that it has to stop?
I searched a bit about it and found that in general, the program tells the O.S that is has executed successfully after which the O.S updates the Program counter to the next address of the next program. But I am not sure how this works in a 6502 processor.

Basically I am trying to make a 6502 emulator and wants to understand the termination logic for terminating the instruction fetch-decode-execute loop. Should I simply add a special instruction at the end of the program, and stop my loop when I encounter it? Or is there any separate instruction to halt the processor execution cycle.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: How does a 6502 processor know when to stop F-D-E-U cycl

Post by GARTHWILSON »

Not counting the STP (stop) and WAI (wait) instructions which the original 6502 and even early 65c02's didn't have, you don't normally stop it.  When a routine ends, it's usually with an RTS (ReTurn from Subroutine) instruction which takes the address off the stack to know where to go back to, that is, to resume execution with the next instruction in the routine that called it, or if it was the startup routine, it might go into an infinite loop where it's just watching for an instruction or condition that will tell it what to do next.  When I was in that initial 6502 class in 1982, we used AIM-65 computers in the lab class, and we were instructed to use the built-in monitor program.  We were to end our little programs with the BRK (BReaK) instruction which went back to the monitor.  I have never used BRK since then though.
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?
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: How does a 6502 processor know when to stop F-D-E-U cycl

Post by BigEd »

For an emulator, using BRK is probably easiest.

Next easiest, perhaps, is for the emulator to use the stack pointer value to recognise a final RTS as exiting from the program.

As noted, a real 6502 doesn't have a way to stop running. So a very faithful emulator will emulate that: perhaps by emulating a system which has a monitor program, so the user will use the monitor to load and run a program, and the program will return to the monitor.

Another simple idiom which you can use on any system is a branch-to-self or jump-to-self which ends the program in a tight loop. This is workable on any system which allows you to reset (or interrupt) the CPU.
Post Reply