Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=========
- //---------
- // JSR
- //---------
- //=========
- //Switch case for JSR
- case 0x20:
- cv.M = sr.AM_Absolute(); //Returns a 16-bit address after PC (used to set PC to target location)
- sr.OP_JSR(cv.M); //Give it to OP_JSR
- cv.cycle -= 6;
- break;
- //Absolute addressing mode
- public ushort AM_Absolute()
- {
- ushort result = cv.memory[cv.PC + 2]; //Read high byte for address
- result = (ushort)(result << 8); //Shift to right position
- result += cv.memory[cv.PC + 1]; //Add low byte
- return result;
- }
- //JSR instruction
- public void OP_JSR(ushort addr)
- {
- ushort temp = (ushort)(cv.PC + 2); //Get return address
- PushWord(temp); //Push it to stack
- cv.PC = addr; //Set PC to target location
- }
- //Push word to stack
- public void PushWord(ushort w)
- {
- byte hi = (byte)((w & 0xFF00) >> 8); //Get high byte and shift it to right place
- cv.memory[0x0100 + cv.S] = hi; //Store value to stack
- cv.S--; //Decrement stack pointer
- byte lo = (byte)(w & 0x00FF); //Get low byte and shift it to right place
- cv.memory[0x0100 + cv.S] = lo; //Store value to stack
- cv.S--; //Decrement stack pointer
- }
- //=========
- //---------
- // RTS
- //---------
- //=========
- //Switch case for RTS
- case 0x60:
- sr.OP_RTS(); //Call OP_RTS
- cv.cycle -= 6;
- break;
- //RTS instruction
- public void OP_RTS()
- {
- byte lo = PullStack(); //Pull low byte from stack
- byte hi = PullStack(); //Pull high byte from stack
- ushort result = CombineBytes(lo, hi); //Combine them to a word
- result++; //Increment result to get to next instruction
- cv.PC = result; //Set PC to return address
- }
- //Pull a byte from stack
- public byte PullStack()
- {
- byte b = cv.memory[0x0100 + cv.S]; //Read value from stack
- cv.S++; //Increment stack pointer
- return b;
- }
- //Combine bytes to word
- public ushort CombineBytes(byte lo, byte hi)
- {
- ushort result;
- result = hi; //Set high byte
- result = (ushort)(result << 8); //Shift it to right place
- result += lo; //Add low byte
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement