6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 23, 2024 2:47 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Nov 09, 2023 8:19 pm 
Offline

Joined: Thu Oct 26, 2023 5:39 pm
Posts: 5
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 09, 2023 8:34 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8545
Location: Southern California
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?


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 10, 2023 8:08 am 
Online
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10986
Location: England
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 23 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: