6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 8:46 am

All times are UTC




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sat May 29, 2021 2:20 pm 
Offline

Joined: Thu Jan 07, 2021 1:29 pm
Posts: 14
I've looked at several 65C02 documents, but they all seem to disagree on the instruction timings. Today I got bored and wrote code and counted cycles. Attached is a document formatted to my preferences with my findings.
This was run on real HW, a Teensy 4.1 simulating memory and controlling a brand new W65C02S6TPG-14. The Teensy spits out a log containing address, data, r/w and opcode fetch. Some manual work to read the log and transcribe to the document, but worked very well.

EDIT: updated the document to v0.2.1


Attachments:
W65C02 opcode summary v0.2.1.txt [32.79 KiB]
Downloaded 137 times

_________________
/NollKollTroll


Last edited by nollkolltroll on Thu Jun 03, 2021 8:22 am, edited 2 times in total.
Top
 Profile  
Reply with quote  
PostPosted: Sat May 29, 2021 2:52 pm 
Online

Joined: Sun Jun 29, 2014 5:42 am
Posts: 352
Nice document...

I think there is an error with the flags on BIT <immediate> (opcode 89).

This only updates the Z flag; the N/V flags are unchanged.

Dave


Top
 Profile  
Reply with quote  
PostPosted: Sat May 29, 2021 4:13 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
hoglet wrote:
I think there is an error with the flags on BIT <immediate> (opcode 89).

This only updates the Z flag; the N/V flags are unchanged.

Good catch. BIT only affects N and V if operating on memory.

Incidentally, in years past, WDC published this information in the 65C02 data sheet (see attached, starting at page 32). Dunno why they stopped doing it.

Attachment:
File comment: W65C02S Data Sheet from March 2000
65c02_2000.pdf [492.05 KiB]
Downloaded 88 times

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Sun May 30, 2021 3:39 am 
Offline

Joined: Thu Jan 07, 2021 1:29 pm
Posts: 14
Thanks for the report, I'll make sure to test it on my setup and update the doc. I've read 3 different versions of WDCs docs and 5-6 other, and they all seem to think that BIT imm DOES transfer bit 6 and 7.
Trying to get proper docs for the 65C02 has been such a frustrating exercise, the only upside was that I KNEW the info was flawed since the different sources disagreed on so many points.
Since I started my project with simulating an NMOS 6502, I'm begining to wonder if a simulated CMOS 65C02 is possible to get right with the level of messy documentation? I'm glad I now run a real CPU, which gets it right no matter what :D

_________________
/NollKollTroll


Top
 Profile  
Reply with quote  
PostPosted: Sun May 30, 2021 3:58 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
The "Bible" is the excellent programming manual, "Programming the 65816 including the 6502, 65C02, and 65802" by David Eyes and Ron Lichty, available at http://wdc65xx.com/Programming-Manual/ . The page in the "Instruction sets" section for BIT specifically says that immediate addressing mode does not do the N and V flags like the other addressing modes do; only the Z.

_________________
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: Sun May 30, 2021 4:32 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
nollkolltroll wrote:
Since I started my project with simulating an NMOS 6502, I'm begining to wonder if a simulated CMOS 65C02 is possible to get right with the level of messy documentation?

Achieving exact simulation is tricky and trying to achieve exactness from what can be gleaned from a data sheet is trickier.

A popular 65xx simulator around here is Mike Kowalski's software. Some time ago, forum denizen 8Bit figured out that there were some errors in the simulation, especially with regard to the number of Ø2 cycles required to execute instructions. So he undertook to fix up the simulator, which snowballed to significant upgrades, the most complicated being the addition of 65C816 support to the assembler.

You can download the latest version here.


GARTHWILSON wrote:
The "Bible" is the excellent programming manual, "Programming the 65816 including the 6502, 65C02, and 65802" by David Eyes and Ron Lichty, available at http://wdc65xx.com/Programming-Manual.

The Eyes & Lichty tome is my first recommendation to anyone learning the 65xx assembly language.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Sun May 30, 2021 5:13 am 
Offline

Joined: Thu Jan 07, 2021 1:29 pm
Posts: 14
A test showed that it is indeed correct that BIT immediate only changes the Z-flag. Updated doc added to first post.
I'll have a look at that book, I only read summary documents which was what I needed. Will be interesting to see how the book matches my findings :)

_________________
/NollKollTroll


Top
 Profile  
Reply with quote  
PostPosted: Sun May 30, 2021 6:05 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
nollkolltroll wrote:
I'll have a look at that book. I only read summary documents which was what I needed.

The description on that page does not do it justice. This is definitely the best 65xx programming manual available, and a must-have for every 65xx programmer! It starts with the basics, followed by architecture, the CMOS 65c02's many improvements over the original NMOS 6502 including added instructions and addressing modes and fixing the NMOS's bugs and quirks, and then the natural progression to the 65816; a thorough tutorial, writing applications, then very detailed and diagrammed information on all 34 addressing modes, at least a page of very detailed description for each instruction, with info on every addressing mode available for that instruction, then instruction lists, tables, and groups, of all 255 active op codes, plus more. 469 pages. From Western Design Center. (.pdf) Note: There were many problems with the earlier .pdf version that were not in the original paper manual; but in late March 2015, WDC scanned and OCR'ed the paper manual and posted the new, repaired .pdf.

_________________
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: Sun May 30, 2021 6:50 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
nollkolltroll wrote:
A test showed that it is indeed correct that BIT immediate only changes the Z-flag. Updated doc added to first post.
I'll have a look at that book, I only read summary documents which was what I needed. Will be interesting to see how the book matches my findings :)

Your description of BIT is still incorrect:

Quote:
Test bits in memory with accumulator
Bits 7 and 6 of operand are transferred
to bit 7 and 6 of S (N,V)
, the zero bit
is set to the result of operand AND
accumulator.

N and V in the status register (which is usually designated SR, not S, in machine language monitors) reflect what was found at the address referred to by the operand, not the operand itself. Only Z is affected when immediate-mode addressing is used, Z always reflecting the result of a logical AND between .A and memory. In the case of immediate mode, "memory" is the actual operand.

Code:
BIT #%10100101     ;logically ANDs the accumulator with %10100101
;
BIT SOMEFLAG       ;logically ANDs the accumulator with whatever is at SOMEFLAG

In both cases, the logical AND only affects SR; the accumulator is unaffected. In the second case, bits 6 and 7 at SOMEFLAG will be copied into V and N, respectively, in SR, but the effect on Z will be the result of the logical AND of the byte at SOMEFLAG with the accumulator.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Sun May 30, 2021 8:53 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
(In the case of 6502 I prefer to call the processor status register P. That matches the opcodes PHP and PLP. And avoids confusion with S, the stack pointer register.)


Top
 Profile  
Reply with quote  
PostPosted: Sun May 30, 2021 9:12 am 
Offline

Joined: Thu Jan 07, 2021 1:29 pm
Posts: 14
BDD: you are correct. I'll have to think about how to phrase it better.

_________________
/NollKollTroll


Top
 Profile  
Reply with quote  
PostPosted: Sun May 30, 2021 12:40 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
A related document can be found in this post.

6502 Dead Cycles.pdf obviously has a bearing on timing. But this detailed listing also explains what addresses appear on the bus during the dead cycles. Besides being of general interest, this matter has relevance in regard to "rogue read" and other problems that can arise in regard to use of certain certain IO devices if they're mapped to an address which can be erroneously touched.

The title mentions 6502, but the 'C02 is also covered. And the 65C816 supposedly produces the same dead-cycle addresses as the 6502, although we didn't verify this. (The 816's VDA signal is always low during dead cycles. Therefore the problems can be prevented if the glue logic requires VDA high in order for the IO device to become selected.)

-- 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: Sun May 30, 2021 7:59 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
Dr Jefyll wrote:
The title mentions 6502, but the 'C02 is also covered. And the 65C816 supposedly produces the same dead-cycle addresses as the 6502, although we didn't verify this.

If the VDA and VPA signals are disregarded in a design, the 65C816 will exhibit the same bus behavior as the NMOS branch of the family, a result of demands made by Apple back when the 816 was on the drawing board. It had to do with the behavior of Apple's disk controller, whose design was (foolishly, in my opinion) predicated on the presence of the so-called dead cycles.

VDA alone does not indicate if the 816 is in a dead cycle. If the logical OR of VDA and VPA is false then the cycle is "dead" and the address bus is in an indeterminate state (also, the address and data buses may be claimed by a different device). In the same vein, if the logical AND of those signals is true, it means the 816 is fetching an opcode.

BigEd wrote:
(In the case of 6502 I prefer to call the processor status register P. That matches the opcodes PHP and PLP. And avoids confusion with S, the stack pointer register.)

However, all of the historically-significant 6502 machine language monitors use SP to represent the stack pointer and SR to represent the status register. Those are mnemonic in nature, whereas using single-letter designations is less so, in my opinion. See here and here for two examples from Jim Butterfield's Supermon for the Commodore 64, perhaps the most widely-used M/L monitor of all time.

Furthermore, the seminal book Machine Language for Beginners has this in it:

    Machine Language Registers

    5. See the registers: type R and you will see something like this on screen (the particular numbers in each category will depend on what's going on in your computer whenever you type R):

    PC IRQ SR AC XR YR SP
    2000 E62E 30 00 05 FF FE

The above is from chapter 3. Note that Machine Language for Beginners was not machine-specific—it covered the most popular 6502 systems in use at the time of publication.

In writing Supermon 816, I followed the historical pattern of using two-character register designations, with DB representing the data bank register, DP representing the direct page pointer register and PB representing the program bank register (the accumulator is represented by .C, since C is the designation for its 16-bit persona—consider instructions such as TCD and TSC, which are 16-bit transfers).

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Mon May 31, 2021 1:23 am 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
BigDumbDinosaur wrote:
VDA alone does not indicate if the 816 is in a dead cycle.
Yes, we agree on that point. As well as during dead cycles, VDA is also low during program fetches other than opcodes fetches. In other words, VDA being low is ambiguous. It could mean either a dead cycle or a Program Fetch Other than an Opcode fetch. (In case anyone needs a reminder, every 65xx instruction includes one opcode byte and zero or more PFOO's, aka operand bytes. And, the 816's VPA/VDA signals vary according to four categories of bus cycle I'll call opcode fetch, PFOO fetch, data fetch/store, and dead cycle. )

Now here's the thing. VDA being low is ambiguous. But for an IO device, the ambiguity doesn't matter. I suggested the glue logic should require VDA high in order for the IO device to become selected, and that means dead cycles will result in de-selection, just as we require. De-selection will also result from PFOO's... but this is not a problem! The ambiguity doesn't matter as long as we're dealing with IO devices only.

I think the confusion arises when the go/no-go signal is applied at a point (such as a decoder) which controls both IO and memory... and I believe your POC's fit this description. Now you do need a go/no-go signal which identifies dead cycles unambiguously. A moment ago I said it's alright for IO to be de-selected during PFOO's, but the same isn't true for memory! And if IO and memory are both subject to a single go/no-go signal, then the logic generating that signal does need to consider both VPA and VDA. So we agree on that, too.

But with different design decisions the circumstances become more relaxed. If the go/no-go signal is applied at a point which only controls IO then VPA can be ignored, and that makes things faster and simpler. For example, 65xx peripheral chips can be effectively protected, with no extra gates and no added propagation delay, simply by driving their active-high Chip Selects with VDA.

-- Jeff


Attachments:
easiest use of VDA.png
easiest use of VDA.png [ 15.15 KiB | Viewed 1668 times ]

_________________
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: Mon May 31, 2021 6:41 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
Dr Jefyll wrote:
Yes, we agree on that point...

This is one of those "your mileage may vary" situations.

As Jeff noted, it all depends on how you are decoding addresses. In my POC units, VDA and VPA are ORed to produce a single high-true signal (VADR) that tells the glue logic the 65C816 is emitting a valid address. The prop delay for that gate is small, under 8ns, worst-case. I got POC V1.2 running in a stable fashion at 20MHz with wait-stating, and POC V1.1 running at 14 MHz without wait-stating, so it could be concluded the slight lag before VADR goes true once the 816 is emitting a valid address is not a concern.

I found it convenient in my designs to go this route. As Jeff notes, there are other ways to do it. That said, I don't believe the prop time of a single OR gate is an issue at any speed at which the 65C816 will be usable. WDC says in the data sheet that all memory and I/O accesses should be qualified by VDA and VPA. I'm merely doing what they recommend.

One thing Jeff and I definitely agree on is the need to qualify I/O accesses with at least VDA. As I previously noted, not accounting for VDA and VPA in glue logic design will result in a system whose bus behavior is like that of the NMOS 6502, which can give rise to difficult-to-diagnose hardware maladies.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: Google [Bot] 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: