Skip to content

Commit

Permalink
#40: every byte and ushort has converted to int from CPU
Browse files Browse the repository at this point in the history
  • Loading branch information
saku-kaarakainen committed Sep 4, 2016
1 parent 672c91b commit c8edcf9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 76 deletions.
28 changes: 14 additions & 14 deletions WinBoyEmulator/GameBoy/CPU/IRegisters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ namespace WinBoyEmulator.GameBoy.CPU
interface IRegisters
{
// 8-bit registers
byte A { get; set; }
byte B { get; set; }
byte C { get; set; }
byte D { get; set; }
byte E { get; set; }
byte F { get; set; }
byte H { get; set; }
byte L { get; set; }
int A { get; set; }
int B { get; set; }
int C { get; set; }
int D { get; set; }
int E { get; set; }
int F { get; set; }
int H { get; set; }
int L { get; set; }

// 16-bit registers
ushort AF { get; set; }
ushort BC { get; set; }
ushort DE { get; set; }
ushort HL { get; set; }
int AF { get; set; }
int BC { get; set; }
int DE { get; set; }
int HL { get; set; }

ushort SP { get; set; }
ushort PC { get; set; }
int SP { get; set; }
int PC { get; set; }
}
}
2 changes: 1 addition & 1 deletion WinBoyEmulator/GameBoy/CPU/Instruction set/IInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace WinBoyEmulator.GameBoy.CPU.Instruction_set
public interface IInstruction
{
/// <summary>Opcode as byte value.</summary>
byte Value { get; set; }
int Value { get; set; }
/// <summary>Length in bytes. Either 1 or 2.</summary>
int Length { get; set; }
/// <summary>Duration in cycles.</summary>
Expand Down
2 changes: 1 addition & 1 deletion WinBoyEmulator/GameBoy/CPU/Instruction set/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace WinBoyEmulator.GameBoy.CPU.Instruction_set
public class Instruction : IInstruction
{
/// <summary>Opcode as byte value.</summary>
public byte Value { get; set; }
public int Value { get; set; }
/// <summary>Length in bytes. Either 1 or 2.</summary>
public int Length { get; set; }
/// <summary>Duration in cycles.</summary>
Expand Down
71 changes: 25 additions & 46 deletions WinBoyEmulator/GameBoy/CPU/LR35902.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ public class LR35902 : Flag, IRegisters
private bool _isCpuRunning = false;

// store registers values to these bytes.
private byte _a, _b, _c, _d, _e, _f, _h, _l;
private ushort _sp, _pc;
private int _a, _b, _c, _d, _e, _f, _h, _l, _sp, _pc;

#region Registers Accessors
/// <summary>8-bit register A. Value between 0x8000 - 0x0100.</summary>
public byte A
public int A
{
get
{
Expand All @@ -55,7 +54,7 @@ public byte A
}
}
/// <summary>8-bit Flag register F. Value between 0x0080 - 0x0000.</summary>
public byte F
public int F
{
get
{
Expand All @@ -67,7 +66,7 @@ public byte F
}
}
/// <summary>16-bit register AF. Combined register A with register F.</summary>
public ushort AF
public int AF
{
get
{
Expand All @@ -81,7 +80,7 @@ public ushort AF
}

/// <summary>8-bit register B. Value betweem 0x8000 - 0x0100.</summary>
public byte B
public int B
{
get
{
Expand All @@ -93,7 +92,7 @@ public byte B
}
}
/// <summary>8-bit register C. Value between 0x0080 - 0x0000.</summary>
public byte C
public int C
{
get
{
Expand All @@ -105,7 +104,7 @@ public byte C
}
}
/// <summary>16-bit register BC. Combined register B with register C.</summary>
public ushort BC
public int BC
{
get
{
Expand All @@ -119,7 +118,7 @@ public ushort BC
}

/// <summary>8-bit register D. Value between 0x8000 - 0x0100.</summary>
public byte D
public int D
{
get
{
Expand All @@ -131,7 +130,7 @@ public byte D
}
}
/// <summary>8-bit register E. Value between 0x0080 - 0x0000.</summary>
public byte E
public int E
{
get
{
Expand All @@ -143,7 +142,7 @@ public byte E
}
}
/// <summary>16-bit register DE. Combined register D with register E.</summary>
public ushort DE
public int DE
{
get
{
Expand All @@ -157,7 +156,7 @@ public ushort DE
}

/// <summary>8-bit register H. Value between 0x8000 - 0x0100.</summary>
public byte H
public int H
{
get
{
Expand All @@ -169,7 +168,7 @@ public byte H
}
}
/// <summary>8-bit register L. Value between 0x0080 - 0x0000.</summary>
public byte L
public int L
{
get
{
Expand All @@ -181,7 +180,7 @@ public byte L
}
}
/// <summary>16-bit register HL. Combined register H with register L.</summary>
public ushort HL
public int HL
{
get
{
Expand All @@ -195,7 +194,7 @@ public ushort HL
}

/// <summary>16-bit Stack Pointer register</summary>
public ushort SP
public int SP
{
get
{
Expand All @@ -208,7 +207,7 @@ public ushort SP
}

/// <summary>16-bit Program Counter. Initialize value 0x100.</summary>
public ushort PC
public int PC
{
get
{
Expand All @@ -228,18 +227,16 @@ public ushort PC

// #endregion

// I am using different methods for setting 8-bit and 16-bit value.
// Reason is that, then I don't have to do any extra casting.

/// <summary>
/// Set byte to 8-bit register.
/// Set value to register.
/// </summary>
/// <param name="register">Register. Use a const string of a static class Register.</param>
/// <param name="value">8-bit (byte) value</param>
private void _setByteToRegister(string register, byte value)
/// <param name="value"></param>
private void _setValueToRegister(string register, int value)
{
switch(register)
{
// 8-bit
case Register.A: A = value; break;
case Register.B: B = value; break;
case Register.C: C = value; break;
Expand All @@ -248,20 +245,8 @@ private void _setByteToRegister(string register, byte value)
case Register.F: F = value; break;
case Register.H: H = value; break;
case Register.L: L = value; break;
default:
throw new ArgumentException("Register doesn't match with 16-bit register", nameof(register));
}
}

/// <summary>
/// Set value to 16-bit register.
/// </summary>
/// <param name="register">Register. Use a const string of a static class Register.</param>
/// <param name="value"></param>
private void _setValueToRegister(string register, ushort value)
{
switch(register)
{
// 16-bit
case Register.AF: AF = value; break;
case Register.BC: BC = value; break;
case Register.DE: DE = value; break;
Expand All @@ -273,10 +258,11 @@ private void _setValueToRegister(string register, ushort value)
}
}

private byte _getByteFromRegister(string register)
private int _getValueFromRegister(string register)
{
switch (register)
{
// 8-bit
case Register.A: return A;
case Register.B: return B;
case Register.C: return C;
Expand All @@ -285,15 +271,8 @@ private byte _getByteFromRegister(string register)
case Register.F: return F;
case Register.H: return H;
case Register.L: return L;
default:
throw new ArgumentException("Register doesn't match with 8-bit register", nameof(register));
}
}

private ushort _getValueFromRegister(string register)
{
switch (register)
{
// 16-bit
case Register.AF: return AF;
case Register.BC: return BC;
case Register.DE: return DE;
Expand Down Expand Up @@ -345,8 +324,8 @@ private void _executeOperand(Instruction opcode)
private void _ld(Instruction opcode)
{
// doens't handle 16-bit registers yet.
var value = _getByteFromRegister(opcode.Source);
_setByteToRegister(opcode.Destination, value);
var value = _getValueFromRegister(opcode.Source);
_setValueToRegister(opcode.Destination, value);
}

/* LDSPnn: function()
Expand Down
28 changes: 14 additions & 14 deletions WinBoyEmulator/GameBoy/CPU/Registers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,38 @@ namespace WinBoyEmulator.GameBoy.CPU.Regiser // Register, Why not Register?
public class Register : IRegisters
{
/// <summary>8-bit register A. Value between 0x8000 - 0x0100.</summary>
public byte A { get; set; }
public int A { get; set; }
/// <summary>8-bit Flag register F. Value between 0x0080 - 0x0000.</summary>
public byte F { get; set; }
public int F { get; set; }
/// <summary>16-bit register AF. Combined register A with register F.</summary>
public ushort AF { get; set; }
public int AF { get; set; }

/// <summary>8-bit register B. Value betweem 0x8000 - 0x0100.</summary>
public byte B { get; set; }
public int B { get; set; }
/// <summary>8-bit register C. Value between 0x0080 - 0x0000.</summary>
public byte C { get; set; }
public int C { get; set; }
/// <summary>16-bit register BC. Combined register B with register C.</summary>
public ushort BC { get; set; }
public int BC { get; set; }

/// <summary>8-bit register D. Value between 0x8000 - 0x0100.</summary>
public byte D { get; set; }
public int D { get; set; }
/// <summary>8-bit register E. Value between 0x0080 - 0x0000.</summary>
public byte E { get; set; }
public int E { get; set; }
/// <summary>16-bit register DE. Combined register D with register E.</summary>
public ushort DE { get; set; }
public int DE { get; set; }

/// <summary>8-bit register H. Value between 0x8000 - 0x0100.</summary>
public byte H { get; set; }
public int H { get; set; }
/// <summary>8-bit register L. Value between 0x0080 - 0x0000.</summary>
public byte L { get; set; }
public int L { get; set; }
/// <summary>16-bit register HL. Combined register H with register L.</summary>
public ushort HL { get; set; }
public int HL { get; set; }

/// <summary>16-bit Stack Pointer register</summary>
public ushort SP { get; set; }
public int SP { get; set; }

/// <summary>16-bit Program Counter. Initialize value 0x100.</summary>
public ushort PC { get; set; } = 0x100;
public int PC { get; set; } = 0x100;

/// <summary>Private constructor to prevent a construction (alone) of this class.</summary>
private Register() { }
Expand Down

0 comments on commit c8edcf9

Please sign in to comment.