Why is the operation address increased by two?

I am watching a Javascript emulator from NES to try to understand how this works.

In this line :

addr = this.load(opaddr+2); 

The operation code is increased by two. However, the documentation (see Appendix E) I read:

When accessing the zero page, one operand is used, which serves as a pointer to the address on the zero page (0000- $ 00FF), where the data to be used can be found. Using zero page addressing, only one byte is needed for the operand, so the instruction is shorter and therefore faster than with addressing modes that accept two operands. An example of a zero page instruction is AND $ 12.

So, if the argument of the operand is only one byte, should it not appear immediately after it and be + 1 instead of + 2? Why is +2?

This is how I think it works, which may be wrong. Suppose our memory looks like this:

 ------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | <- index ------------------------- | a | b | c | d | e | f | <- memory ------------------------- ^ \ PC 

and our PC is 0 , pointing to a . For this loop, we say that the operation code is:

 var pc= 0; //for example sake var opcode= memory[pc]; //a 

So the first operand should not be the next slot, i.e. b ?

 var first_operand = memory[pc + 1]; //b 
+6
source share
1 answer

Your analysis seems right at first glance, but since the emulator is working, there must be something else going on.

The corresponding code is as follows:

  var opinf = this.opdata[this.nes.mmap.load(this.REG_PC+1)]; var cycleCount = (opinf>>24); var cycleAdd = 0; // Find address mode: var addrMode = (opinf >> 8) & 0xFF; // Increment PC by number of op bytes: var opaddr = this.REG_PC; this.REG_PC += ((opinf >> 16) & 0xFF); var addr = 0; switch(addrMode){ case 0:{ // Zero Page mode. Use the address given after the opcode, // but without high byte. addr = this.load(opaddr+2); break; 

Please note, as shown in the first line, memory access for information about the instruction is located at REG_PC+1 . Thus, the PC actually points to the byte of the previous executable operation code, so the operands start at this address + 2. The code itself is encoded as the lower 8 bytes of opinf and is used in execution to switch the page or so below the code segment shown.

+2
source

All Articles