6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Sep 27, 2024 5:16 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Jun 16, 2022 3:40 am 
Offline

Joined: Sun Oct 03, 2021 2:17 am
Posts: 114
My copy of the Scanlon book, 6502 Software Design, is in. I was just thrown for a loop by the last sentence in this paragraph on page 18, so I'm posting my thoughts here in case it trips anyone else up (bold emphasis mine):

Quote:
Instructions are comprised of one, two, or three bytes. The first byte always holds the machine-code equivalent of the operation code (op code), so this byte is directed into the instruction register and routed to the instruction decode logic. The instruction decode logic issues appropriate internal-control signals to all other elements of the microprocessor,
and possibly to external circuits in the microcomputer system. The second and third bytes, if the instruction has them, are gated into the data bus buffer, from which they are routed into either the Arithmetic Logic Unit (ALU) if they represent data, or into the program counter if they represent an address.


Huh? It seems to claim that for (multi-byte) instructions that contain an address, the address goes into the program counter? If that's the case, then LDA $0000 would set the PC to $0000 instead of loading the value at $0000 into A. Or is $0000 considered "data" and goes into the ALU in this case? I think only the various jump and branch instructions are the kinds of address-carrying instructions the author was thinking of that sometimes change the PC. But many instructions, like shifts, rotates, loads, and stores, etc., do not otherwise change the PC apart of allowing it to increment to the next instruction. Is my understanding correct?


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 16, 2022 4:04 am 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
jeffythedragonslayer wrote:
I think only the various jump and branch instructions are the kinds of address-carrying instructions the author was thinking of that sometimes change the PC. But many instructions, like shifts, rotates, loads, and stores, etc., do not otherwise change the PC apart of allowing it to increment to the next instruction. Is my understanding correct?
Yes. :)

-- Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 16, 2022 6:43 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10938
Location: England
I think I remember, when first getting to know the 6502 (the first micro I did get to know) having a little bit of confusion between the Program Counter and the Address Bus. At the point of fetching the first byte of each instruction, the PC value is driven to the address bus, and that's fine. But in the subsequent cycles, as you note, the address bus may be used for read or write accesses according to what the instruction is, and it does not reflect the PC value.

I think what's happening is a bit of confusion between a very software way of looking at code, and a more hardware way of understanding what the system is doing. In particular, thinking instruction-by-instruction is one thing, and thinking cycle-by-cycle is another.

In my experience, that confusion goes away eventually. It's best not to write a book while confused!

(It is true for JMP and JSR that the two bytes of operand will find their way to the PC. So the sentence might also be fixable if it talked about a program address, rather than an address.)


Top
 Profile  
Reply with quote  
PostPosted: Sat Jun 18, 2022 2:13 pm 
Offline
User avatar

Joined: Mon Apr 23, 2012 12:28 am
Posts: 760
Location: Huntsville, AL
I think Ed's comment about the software vs. hardware view of the process is on target. The sentence in question causes some confusion by not identifying that the second and third bytes must be held in internal registers / latches that are not visible to the programmer. It is from these internal registers / latches that the data is routed as described in the referenced text. Given the two phase nature of the 6502 machine cycle, it is possible to reduce the number of temporary registers to reduce the transistor count. Since latches are the primary memory devices used within the 6502, it is possible to transfer one operand, from a single internal temporary operand latch, to the low address latch, and simultaneously transfer the second byte of the address from a data input buffer, implemented as a latch, to the high address latch. Simultaneously, this 16-bit address can be loaded into the program counter. Refer to the 6502 block diagram provided at this link.

This flow through feature of latches can be used very effectively to reduce the number of memory elements required. However, it requires precise control of the latch control signals, which can be difficult to achieve when high rates, or short delays, are required. Thus, modern design practices avoid the use of latches and instead rely on registers, which do not allow the flow through action described above.

In the case of my 6502/65C02 FPGA-based emulation, I use two registers which I name OP1 and OP2, which hold the first and second operand bytes of an instruction, respectively. When I load OP1, my implementation either zeros OP2, or loads it with all 1s based on the most significant bit of the operand byte being loaded into OP1, i.e., sign extension. OP2 is the sign-extension of the OP1 for all branch instructions, and simplifies the implementation of the PC-relative addressing associated with the 6502/65C02 branch instructions.

When I "execute" the instruction after all required operands are present in the operand registers, my implementation effectively performs the actions described in the referenced text: OP2, OP1 are either used to modify the PC, generate a direct / indirect operand address with or without indexing, or supply an immediate data to the ALU which may simply pass it through to the A, X, Y, or P registers.

_________________
Michael A.


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

All times are UTC


Who is online

Users browsing this forum: AndrewP and 9 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: