Skip to content

Commit

Permalink
- Finished Unit Tests
Browse files Browse the repository at this point in the history
- Fixed Opcode 8XY5 and 8XY7 Bug
  • Loading branch information
Darryl Day committed Oct 4, 2020
1 parent e1e85f4 commit bab364c
Show file tree
Hide file tree
Showing 4 changed files with 523 additions and 51 deletions.
33 changes: 17 additions & 16 deletions Assets/Scripts/Core/CPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Chip8Sharp.Input;
using Chip8Sharp.Debug;
using Chip8Sharp.Sound;
using System;

namespace Chip8Sharp.Core
{
Expand Down Expand Up @@ -79,6 +80,8 @@ public CPU(
_beep = beep;
_randomNumber = randomNumber;
_logger = logger;

Reset();
}

public void Reset()
Expand Down Expand Up @@ -287,15 +290,15 @@ public void EmulateCycle()

// VY is subtracted from VX. VF is set to 0 when there's a borrow, and 1 when there isn't.
int subVxVy = V[X] - V[Y];

if (subVxVy < 0)
{
subVxVy += 256;
V[F] = 0x01;
V[F] = 0x00;
}
else
{
V[F] = 0x00;
V[F] = 0x01;
}

V[X] = (byte)subVxVy;
Expand All @@ -316,11 +319,11 @@ public void EmulateCycle()
if (subVyVx < 0)
{
subVyVx += 256;
V[F] = 0x01;
V[F] = 0x00;
}
else
{
V[F] = 0x00;
V[F] = 0x01;
}

V[X] = (byte)subVyVx;
Expand Down Expand Up @@ -519,16 +522,13 @@ public void EmulateCycle()
break;
}

if (!_keyPressHault)
if (skipNextInstruction)
{
PC += 4;
}
else if (incrementCounter)
{
if (skipNextInstruction)
{
PC += 4;
}
else if (incrementCounter)
{
PC += 2;
}
PC += 2;
}

if (drawFlag)
Expand Down Expand Up @@ -556,12 +556,13 @@ public void EmulateCycle()

}

public void SetKeys()
public void SetKeys(bool keysOverridden = false)
{
bool keyActive = false;
byte keyNumber = 0;

_userInput.SetKeys(Keys);
if (!keysOverridden)
_userInput.SetKeys(Keys);

// Check if a key is active
for (byte i = 0; i < 16; i++)
Expand Down
63 changes: 41 additions & 22 deletions Assets/Scripts/Unity/UnityChip8SharpEmu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ public class UnityChip8SharpEmu : MonoBehaviour
[SerializeField] private Texture2DScreenRenderer _unityScreenRenderer;
[SerializeField] private UnityBeep _unityBeep;

public bool StepThrough = false;

private UnityUserInput _unityUserInput;
private UnityRandomNumber _unityRandomNumber;
private UnityLogger _unityLogger;

public CPU CPU { get; private set; }

public bool InitOnStart { get; set; } = true;

private bool _gameLoaded = false;
private float _deltaCounter;

Expand All @@ -32,38 +36,53 @@ public void Init()
_unityRandomNumber,
_unityLogger
);

//CPU.Reset();
}

void Start()
{
//FileBrowser.SetFilters(false, ".ch8");
//FileBrowser.ShowLoadDialog((files) =>
//{
// var romBytes = System.IO.File.ReadAllBytes(files[0]);
// CPU.LoadGame(romBytes);
// _gameLoaded = true;
//},
//() => { },
//title: "Select Chip 8 Rom");

if (InitOnStart)
{
Init();

FileBrowser.SetFilters(false, ".ch8");
FileBrowser.ShowLoadDialog((files) =>
{
var romBytes = System.IO.File.ReadAllBytes(files[0]);
CPU.LoadGame(romBytes);
_gameLoaded = true;
},
() => { },
title: "Select Chip 8 Rom");
}
}

void Update()
{
//_deltaCounter += Time.deltaTime;
if (StepThrough)
{
if (UnityEngine.Input.GetKeyDown(KeyCode.Space))
{
UnityEngine.Debug.Log(CPU.Opcode.ToString("x"));
CPU.EmulateCycle();
CPU.SetKeys();
}
}
else
{
_deltaCounter += Time.deltaTime;

if (_deltaCounter >= (1f / 500f))
{
_deltaCounter = 0f;

//if (_deltaCounter >= (1f / 500f))
//{
// _deltaCounter = 0f;
if (_gameLoaded)
{
CPU.EmulateCycle();
CPU.SetKeys();
}
}
}

// if (_gameLoaded)
// {
// CPU.EmulateCycle();
// CPU.SetKeys();
// }
//}
}
}
}
Loading

0 comments on commit bab364c

Please sign in to comment.