Skip to content

Commit

Permalink
Write back a MDLX of type 3 and 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Feb 15, 2020
1 parent 651521d commit d2b3391
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 69 deletions.
78 changes: 78 additions & 0 deletions OpenKh.Common/Ps2/Dma.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Xe.BinaryMapper;

namespace OpenKh.Common.Ps2
{
/// <summary>
/// EE User Manual, 6.3.2
/// </summary>
public enum VifOpcode : byte
{
NOP = 0b00000000,
STCYCL = 0b00000001,
OFFSET = 0b00000010,
BASE = 0b00000011,
ITOP = 0b00000100,
STMOD = 0b00000101,
MSKPATH3 = 0b00000110,
MARK = 0b00000111,
FLUSHE = 0b00010000,
FLUSH = 0b00010001,
FLUSHA = 0b00010011,
MSCAL = 0b00010100,
MSCALF = 0b00010101,
MSCNT = 0b00010111,
STMASK = 0b00100000,
STROW = 0b00110000,
STCOL = 0b00110001,
MPG = 0b01001010,
DIRECT = 0b01010000,
DIRECTH = 0b01010001
}

/// <summary>
/// EE User Manual, 6.3.2
/// </summary>
public class VifCode
{
[Data] public byte Cmd { get; set; }
[Data] public byte Num { get; set; }
[Data] public ushort Immediate { get; set; }

public VifOpcode Opcode
{
get => (VifOpcode)(Cmd & 7);
set => Cmd = (byte)((byte)value | (Interrupt ? 0x80 : 0));
}

public bool Interrupt
{
get => (Cmd >> 7) != 0;
set => Cmd = (byte)((byte)Opcode | (value ? 0x80 : 0));
}
}

/// <summary>
/// EE User Manual, 5.6
/// </summary>
public class DmaTag
{
/// <summary>
/// Quadword count; packet size
/// </summary>
[Data] public ushort Qwc { get; set; }
[Data] public ushort Param { get; set; }
[Data] public int Address { get; set; }

public int TagId
{
get => (Param >> 12) & 3;
set => Param = (ushort)(((value & 3) << 12) | (Irq ? 0x8000 : 0));
}

public bool Irq
{
get => (Param >> 15) != 0;
set => Param = (ushort)((TagId << 12) | (value ? 0x8000 : 0));
}
}
}
30 changes: 30 additions & 0 deletions OpenKh.Common/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public static int ReadInt32(this Stream stream) =>
public static uint ReadUInt32(this Stream stream) =>
new BinaryReader(stream).ReadUInt32();

public static long ReadInt64(this Stream stream) =>
new BinaryReader(stream).ReadInt64();

public static ulong ReadUInt64(this Stream stream) =>
new BinaryReader(stream).ReadUInt64();

public static List<int> ReadInt32List(this Stream stream, int offset, int count)
{
stream.Position = offset;
Expand Down Expand Up @@ -111,6 +117,30 @@ public static int Write(this Stream stream, IEnumerable<int> items)
return (int)stream.Position - oldPosition;
}

public static void Write(this Stream stream, byte value) =>
new BinaryWriter(stream).Write(value);

public static void Write(this Stream stream, char value) =>
new BinaryWriter(stream).Write(value);

public static void Write(this Stream stream, short value) =>
new BinaryWriter(stream).Write(value);

public static void Write(this Stream stream, ushort value) =>
new BinaryWriter(stream).Write(value);

public static void Write(this Stream stream, int value) =>
new BinaryWriter(stream).Write(value);

public static void Write(this Stream stream, uint value) =>
new BinaryWriter(stream).Write(value);

public static void Write(this Stream stream, long value) =>
new BinaryWriter(stream).Write(value);

public static void Write(this Stream stream, ulong value) =>
new BinaryWriter(stream).Write(value);

public static void Copy(this Stream source, Stream destination, int length, int bufferSize = 65536)
{
int read;
Expand Down
Loading

0 comments on commit d2b3391

Please sign in to comment.