Jump to content
IGNORED

Why are they illegal?


StanJr

Recommended Posts

Actually, it's an apt enough name. If you perform too many illegal instructions, you could "break the law" of the CPU and crash your program. Also any time a processor is updated for a new generation of hardware, there's a chance that any loopholes which previously allowed illegal opcodes will be closed. I think that's partly why certain homebrews work fine on an old 2600 but flake out when played on a Jr.

Link to comment
Share on other sites

:lol:

 

Just an instruction or line of code that performs an operation not planned in the design of the CPU.  "Illegal" because it therefore shouldn't have been allowed, but works anyway.

 

Actually, the best term is 'undocumented'. The CPU may do something useful in response to them, but their operation was never documented. This may be because their operation was inconsistent or confusing, or because the designer planned on assigning a different function to those opcodes in the future.

 

The 6502 has very simple instruction decode logic (it tries to act on any opcode), and these opcodes create corruptions or combinations of other valid instructions. Some are useful and some are not.

 

-Bry

 

edit: In response to MMF. Not all versions of the 6502(+family) respond the same way to them. This is why they may cause incompatibilities. Some of them will simply lock up the CPU's state machine.

Link to comment
Share on other sites

edit: In response to MMF. Not all versions of the 6502(+family) respond the same way to them. This is why they may cause incompatibilities. Some of them will simply lock up the CPU's state machine.

 

That's what I meant, stated much more elegantly. It's been far too long since I took programming in college. :lolblue:

Link to comment
Share on other sites

A few examples (the names were assigned by programmers):

 

ANC - Corruption of AND

Opcodes: $0B, $2B

Works like AND, but also sets the Carry flag if the result is Negative

 

LAX - Combination of LDA, LDX

Opcodes: $A7, $B7, $AF, $BF, $A3, $B3

Loads A and X with the same value.

 

SRE - Combination of LSR and EOR

Opcodes: $47, $57, $4F, $5F, $5B, $43, $53

Shift a memory location right, then perform an exclusive-OR with the Accumulator.

 

The combinations are possible because normal 6502 instructions do not have the last 2 bits set at once. When you do set both bits, you often get the effect of 2 instructions run back-to-back.

 

You can read more here:

http://www.s-direktnet.de/homepages/k_nadj...dj/opcodes.html

 

-Bry

Link to comment
Share on other sites

Also any time a processor is updated for a new generation of hardware, there's a chance that any loopholes which previously allowed illegal opcodes will be closed.  I think that's partly why certain homebrews work fine on an old 2600 but flake out when played on a Jr.

AFAIK the CPU of the Jr. supports all illegal opcodes that the original 650x CPUs supports too. And I only using very few illegal opcodes (LAX, NOP and DCP) and I am not using so called "unstable" illegal opcodes (which behave differently in different CPUs).

 

So, if a game won't work on a Jr. the reason must be different. Do you have any examples?

Link to comment
Share on other sites

AFAIK the CPU of the Jr. supports all illegal opcodes that the original 650x CPUs supports too. And I only using very few illegal opcodes (LAX, NOP and DCP) and I am not using so called "unstable" illegal opcodes (which behave differently in different CPUs).

 

NOP is an illegal opcode on the 6507? Oh, and I prefer "undocumented" to "illegal" as well. "Illegal" does make them sound more sexy, though. :)

 

..Al

Link to comment
Share on other sites

I'm pretty sure there are some on the Z80 as well.

 

The Z80 has some 'prefix' codes that aren't instructions, but rather they modify the action the next instruction will take, so some instructions are other instructions with a certain prefix code.

 

-Bry

Link to comment
Share on other sites

:?: Was Thrust the first 2600 game to use illegal opcodes?

Probably, though I am not 100% sure. But AFAIK all older games work on the older Stella emulators (<v1.2), so the answer is most likely "yes!"

 

I think the main reason for the original programers not using them was, that they couldn't be 100% sure those opcodes would be supportd by all future 6507 (or replacement) CPUs.

Link to comment
Share on other sites

I thought Robot Tank used one.

 

NOP (code $EA) isn't...but alternate versions that waste varying amounts of cycles are (even tho they work as well), as well as other bit variations of the instruction (1A, 3A, 5A, 7A, DA, FA).  Only NOP (code $EA) is official.

 

Even $EA isn't actually a No-Operation code. It just performs an operation and then doesn't store the result anywhere. The other undocumented NOP codes are all doing the same thing but using different addressing modes for their discarded operation.

 

-Bry

Link to comment
Share on other sites

Well Thomas, what about Vault Assault?  :?:  You never responded. :P

Sorry, I missed your reply.

 

You are refering to thisthread, right?

 

This is no problem with "illegal" opcodes, but with "illegal" programming. Some hardware registeres are accessed during a period where they shouldn't be, causing unpredictable results. Probably a similar problem as with Kool Aid Man.

Link to comment
Share on other sites

Are there other advantages?

They also save some ROM space (by executing two instructions in one and by allowing some nice tricks e.g. with NOP).

 

Any other advantages you had in your mind? There aren't any more I can think about.

 

I can think of 3:

 

Speed - Sometimes you can shave a cycle or two with an Illegal Op

Size - Sometimes you can shave a byte or two as well

Multi-Length NOP's - During timed portions of code, having a 3,4,5 cycle NOP can be useful.

 

Oh yeah, a 4th reason is that they can make you feel like an uber-coder.

 

-Bry

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...