SHARE
TWEET

JSR and RTS implementation

a guest Dec 19th, 2018 7 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=========
  2. //---------
  3. //   JSR
  4. //---------
  5. //=========
  6.  
  7. //Switch case for JSR
  8.                 case 0x20:
  9.                     cv.M = sr.AM_Absolute();     //Returns a 16-bit address after PC (used to set PC to target location)
  10.                     sr.OP_JSR(cv.M);    //Give it to OP_JSR
  11.  
  12.                     cv.cycle -= 6;
  13.                     break;
  14.  
  15. //Absolute addressing mode
  16.     public ushort AM_Absolute()
  17.     {
  18.         ushort result = cv.memory[cv.PC + 2];    //Read high byte for address
  19.         result = (ushort)(result << 8);    //Shift to right position
  20.         result += cv.memory[cv.PC + 1];    //Add low byte
  21.  
  22.         return result;
  23.     }
  24.  
  25. //JSR instruction
  26.     public void OP_JSR(ushort addr)
  27.     {
  28.         ushort temp = (ushort)(cv.PC + 2);     //Get return address
  29.         PushWord(temp);    //Push it to stack
  30.         cv.PC = addr;     //Set PC to target location
  31.     }
  32.  
  33. //Push word to stack
  34.     public void PushWord(ushort w)
  35.     {
  36.         byte hi = (byte)((w & 0xFF00) >> 8);    //Get high byte and shift it to right place
  37.         cv.memory[0x0100 + cv.S] = hi;    //Store value to stack
  38.         cv.S--;    //Decrement stack pointer
  39.  
  40.         byte lo = (byte)(w & 0x00FF);    //Get low byte and shift it to right place
  41.         cv.memory[0x0100 + cv.S] = lo;    //Store value to stack
  42.         cv.S--;    //Decrement stack pointer
  43.     }
  44.  
  45.  
  46.  
  47. //=========
  48. //---------
  49. //   RTS
  50. //---------
  51. //=========
  52.  
  53. //Switch case for RTS
  54.                 case 0x60:
  55.                     sr.OP_RTS();    //Call OP_RTS
  56.  
  57.                     cv.cycle -= 6;
  58.                     break;
  59.  
  60. //RTS instruction
  61.     public void OP_RTS()
  62.     {
  63.         byte lo = PullStack();    //Pull low byte from stack
  64.         byte hi = PullStack();    //Pull high byte from stack
  65.         ushort result = CombineBytes(lo, hi);    //Combine them to a word
  66.         result++;    //Increment result to get to next instruction
  67.         cv.PC = result;    //Set PC to return address
  68.     }
  69.  
  70. //Pull a byte from stack
  71.     public byte PullStack()
  72.     {
  73.         byte b = cv.memory[0x0100 + cv.S];    //Read value from stack
  74.         cv.S++;    //Increment stack pointer
  75.  
  76.         return b;
  77.     }
  78.  
  79. //Combine bytes to word
  80.     public ushort CombineBytes(byte lo, byte hi)
  81.     {
  82.         ushort result;
  83.  
  84.         result = hi;    //Set high byte
  85.         result = (ushort)(result << 8);    //Shift it to right place
  86.         result += lo;    //Add low byte
  87.  
  88.         return result;
  89.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top