Example #1
0
 private void updateTimer()
 {
     if (GameBoyCPU.getBit(6, RTCRegisters[4]) == 0)
     {
         DateTime curr     = System.DateTime.Now;
         TimeSpan currSpan = curr - start;
         RTCRegisters[0] = (byte)(currSpan.Seconds);
         RTCRegisters[1] = (byte)(currSpan.Minutes);
         RTCRegisters[2] = (byte)(currSpan.Hours);
         RTCRegisters[3] = (byte)(currSpan.Days);
         if (currSpan.Days > 255)
         {
             //Rollover
             RTCRegisters[4] = GameBoyCPU.setBit(0, RTCRegisters[4]);
         }
         if (currSpan.Days > 511)
         {
             //Set overflow bit.
             RTCRegisters[4] = GameBoyCPU.setBit(7, RTCRegisters[4]);
             //Unset rollover
             RTCRegisters[4] = GameBoyCPU.resetBit(0, RTCRegisters[4]);
             start           = start.AddDays(512);
         }
     }
 }
Example #2
0
    public bool ResetJoyPadBit(byte key)
    {
        bool isHighToLow = (GameBoyCPU.getBit(5, memory[0xFF00]) == 1) ? true : false;

        joypadState = GameBoyCPU.resetBit(key, GameBoyMemory.joypadState);
        return(isHighToLow);
    }
Example #3
0
    public void setSpeed()
    {
        byte speedBit = getSpeedBit();

        if (speedBit == 0)
        {
            setSpeedBit();
        }
        else
        {
            resetSpeedBit();
        }
        speed = (uint)((GameBoyCPU.getBit(7, memory[KEY1])) == 1 ? 2 : 1); // if 1 we switch to normal speed (1) and if 0 we switch to double speed (2)
    }
Example #4
0
    private void updateControl(byte data)
    {
        DateTime e      = System.DateTime.Now;
        TimeSpan diff   = e - start;
        byte     dayBit = GameBoyCPU.getBit(0, RTCRegisters[4]);

        if (dayBit == 1 && (data & 0x1) == 0)
        {
            start = start.AddDays(256);
        }
        else if (dayBit == 0 && (data & 0x1) == 1)
        {
            start = start.AddDays(-256);
        }
        LatchRegisters[4] = RTCRegisters[4] = data;
    }
Example #5
0
    private void updateDays(ushort dayReset)
    {
        DateTime e    = System.DateTime.Now;
        TimeSpan diff = e - start;

        if (GameBoyCPU.getBit(0, RTCRegisters[4]) == 1)
        {
            dayReset += 256;
        }
        if (dayReset < diff.Days)
        {
            ushort diffDays = (ushort)(diff.Days - dayReset);
            start = start.AddDays(diffDays);
        }
        else if (dayReset > diff.Days)
        {
            ushort diffDays = (ushort)(dayReset - diff.Days);
            start = start.AddDays(-diffDays);
        }
    }
Example #6
0
    private byte getJoyPadState()
    {
        byte res = memory[0xFF00];

        // flip all the bits
        res ^= 0xFF;

        // are we interested in the standard buttons?
        if (GameBoyCPU.getBit(4, res) == 0)
        {
            byte topJoypad = (byte)(joypadState >> 4);
            topJoypad |= 0xF0;                   // turn the top 4 bits on
            res       &= topJoypad;              // show what buttons are pressed
        }
        else if (GameBoyCPU.getBit(5, res) == 0) //directional buttons
        {
            byte bottomJoypad = (byte)(joypadState & 0xF);
            bottomJoypad |= 0xF0;
            res          &= bottomJoypad;
        }
        return(res);
    }
Example #7
0
 public void Write(ushort PC, byte data)
 {
     //RAM Enabled and ROM Bank Number
     if (PC >= 0x0000 && PC <= 0x3FFF)
     {
         byte bit8 = (byte)(GameBoyCPU.getBitFromWord(8, PC));
         if (bit8 != 1)
         {
             byte mask         = (byte)(data & 0x0F);
             bool prevRamState = ramEnable;
             ramEnable = (mask == 0x0A) ? true : false;
             //Went from true to false
             if (prevRamState && !ramEnable && battery)
             {
                 save();
             }
         }
         else
         {
             bank1 = (byte)(data & 0x0F);
             if (bank1 == 0)
             {
                 bank1 = 1;
             }
         }
     }
     else if (PC >= 0xA000 && PC <= 0xBFFF)
     {
         if (ramEnable)
         {
             byte newData = (byte)(data & 0x0F);
             int  offset  = PC - 0xA000;
             ramMemory[offset % 512] = data;
         }
     }
 }
Example #8
0
    void InitalizeComponent()
    {
        Application.targetFrameRate = 60;

        //Create display
        texture = new Texture2D(width, height);
        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, width, height), Vector2.zero, 1);

        sprite.name = "Screen";
        GogbGraphic = GameObject.Find("GameBoyScreen");
        GogbGraphic.GetComponent <SpriteRenderer>().sprite = sprite;
        Vector2 pivotPoints = new Vector2(0.0f, 0.0f);

        GogbGraphic.GetComponent <RectTransform>().pivot = pivotPoints;


        //Load Cartiridge
        gbCart = new GameBoyCartiridge(0);
        try {
            gbCart.LoadRom(pathToRom);
        } catch {
            Debug.Log("NO ROM LOADED");
        }


        //Create memory
        gbMemory = new GameBoyMemory(gbCart);
        bool reset = false;

        try {
            gbMemory.LoadBios(pathToBios);
        } catch {
            reset = true;
        }

        //Create Interrupts
        gbInterrupts = new GameBoyInterrupts(gbMemory);

        //Create Timer
        gbTimer = new GameBoyTimer(gbMemory, gbInterrupts);
        gbMemory.AddTimer(gbTimer);

        //Create CPU
        gbCPU = new GameBoyCPU(gbMemory, gbInterrupts);
        if (reset && !GameBoyCartiridge.IsGameBoyColor)
        {
            gbCPU.ResetGBNoBios();
        }
        else if (reset && GameBoyCartiridge.IsGameBoyColor)
        {
            gbCPU.ResetCGBNoBios();
        }

        //Create GPU
        gbGraphic = new GameBoyGraphic(width, height, texture, gbInterrupts, gbMemory);
        //Create Audio
        gbAudio = new GameBoyAudio();
        gbMemory.AddGraphics(gbGraphic);
        gbMemory.AddAudio(gbAudio);

        //Create Keyboard
        gbJoyPad = new GameBoyJoyPad(gbInterrupts, gbMemory);
        Debug.Log(GameBoyCartiridge.Title);
        Debug.Log(GameBoyCartiridge.IsGameBoyColor);
        Debug.Log(gbCart.CartiridgeType.ToString("X2"));
    }
Example #9
0
 public void SetJoyPadBit(byte key)
 {
     joypadState = GameBoyCPU.setBit(key, GameBoyMemory.joypadState);
 }
Example #10
0
 private byte getSpeedBit()
 {
     return(GameBoyCPU.getBit(7, memory[KEY1]));
 }
Example #11
0
 private void resetSpeedBit()
 {
     memory[KEY1] = GameBoyCPU.resetBit(7, memory[KEY1]);
 }
Example #12
0
 public void unSetPrepared()
 {
     memory[KEY1] = GameBoyCPU.resetBit(0, memory[KEY1]);
 }
Example #13
0
 public bool WriteToMemory(ushort address, byte data)
 {
     if (address >= 0x0000 && address <= 0x7FFF)
     {
         gbCart.Write(address, data);
     }
     else if (address >= 0x8000 && address <= 0x9FFF)
     {
         gbGraphic.Write(address, data);
     }
     else if (address == GameBoyTimer.DIV)
     {
         uint rate     = (byte)(memory[GameBoyTimer.TAC] & 0x3);
         byte divValue = memory[GameBoyTimer.DIV];
         //Failing Edge detector... don't know if this is correct
         if (rate == 0 && GameBoyCPU.getBitFromWord(9, gbTimer.TIMACycleCount) == 1)
         {
             gbTimer.IncrementTIMACheck();
         }
         else if (rate == 1 && GameBoyCPU.getBitFromWord(3, gbTimer.TIMACycleCount) == 1)
         {
             gbTimer.IncrementTIMACheck();
         }
         else if (rate == 2 && GameBoyCPU.getBitFromWord(5, gbTimer.TIMACycleCount) == 1)
         {
             gbTimer.IncrementTIMACheck();
         }
         else if (rate == 3 && GameBoyCPU.getBitFromWord(7, gbTimer.TIMACycleCount) == 1)
         {
             gbTimer.IncrementTIMACheck();
         }
         memory[GameBoyTimer.DIV] = 0;
         gbTimer.resetTimer();
     }
     else if (address == GameBoyTimer.TIMA)
     {
         //if(!gbTimer.isReloadingTIMA()) {
         memory[GameBoyTimer.TIMA] = data;
         //}
     }
     else if (address >= 0xA000 && address <= 0xBFFF)
     {
         gbCart.Write(address, data);
     }
     else if (address == GameBoyGraphic.LYAddr)
     {
         memory[address] = 0;
         gbGraphic.resetWindowLine();
     }
     else if (address >= 0xC000 && address <= 0xFDFF)
     {
         workRam.Write(address, data);
     }
     else if (address == DMA)
     {
         DMATransfer(data);
     }
     else if ((address == 0xFF4F || (address >= 0xFF51 && address <= 0xFF55) ||
               (address >= 0xFF68 && address <= 0xFF6B)) & GameBoyCartiridge.IsGameBoyColor)
     {
         gbGraphic.Write(address, data);
     }
     else if (address >= 0xFF01 && address <= 0xFF02)
     {
         Debug.Log("Link Port");
     }
     else if (address >= 0xFF10 && address <= 0xFF3F)
     {
         gbAudio.Write(address, data);
     }
     else if (address == GameBoyGraphic.STATAddr)
     {
         memory[address] = (byte)((memory[address] & 0x7) | (data & 0xF8));
     }
     else if (address == 0xFF70 && GameBoyCartiridge.IsGameBoyColor)
     {
         workRam.Write(address, data);
     }
     else if (address == KEY1)
     {
         data            = (byte)(data & 0x7f);
         memory[address] = data;
     }
     else
     {
         memory[address] = data;
     }
     return(true);
 }
Example #14
0
    public void RequestInterrupt(byte bit)
    {
        byte ifReg = memory.ReadFromMemory(IF);

        memory.WriteToMemory(IF, GameBoyCPU.setBit(bit, ifReg));
    }