From af5156f6ef1b8b87593b5acd005b13a07baabf36 Mon Sep 17 00:00:00 2001 From: Blue Date: Sat, 29 May 2021 17:44:36 +0200 Subject: [PATCH] Add: Sector, an spanized buffer/queue. The CDROM doesn't need a real tail/head queue/enqueue function, as it only needs to one shot the fill of the sector and then dequeue. Also Queue doesn't have a Span constructor and can't clean and "addAll" without allocating a new internal buffer array. This little util class aims to try to avoid allocations as well as simplify the DMAs out. --- ProjectPSX/Devices/CDROM/Sector.cs | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ProjectPSX/Devices/CDROM/Sector.cs diff --git a/ProjectPSX/Devices/CDROM/Sector.cs b/ProjectPSX/Devices/CDROM/Sector.cs new file mode 100644 index 0000000..b9b196d --- /dev/null +++ b/ProjectPSX/Devices/CDROM/Sector.cs @@ -0,0 +1,42 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace ProjectPSX.Devices.CdRom { + class Sector { + + private const int BYTES_PER_SECTOR_RAW = 2352; + private byte[] sectorBuffer = new byte[BYTES_PER_SECTOR_RAW]; + + private int pointer; + private int size; + + public void fillWith(Span data) { + pointer = 0; + size = data.Length; + var dest = sectorBuffer.AsSpan(); + data.CopyTo(dest); + } + + public ref byte readByte() { + ref var data = ref MemoryMarshal.GetArrayDataReference(sectorBuffer); + return ref Unsafe.Add(ref data, pointer++); + } + + public Span read(int size) { //size from dma comes as u32 + var dma = sectorBuffer.AsSpan().Slice(pointer, size * 4); + pointer += size * 4; + return MemoryMarshal.Cast(dma); + } + + public Span read() => sectorBuffer.AsSpan().Slice(0, size); + + public bool hasData() => pointer < size; + + public void clear() { + pointer = 0; + size = 0; + } + + } +}