56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
using Core.Io;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
namespace Core.Io
|
|
{
|
|
public class TzxParser
|
|
{
|
|
public static List<TzxBlock> Parse(byte[] fileBytes)
|
|
{
|
|
List<TzxBlock> blocks = new List<TzxBlock>();
|
|
|
|
using (MemoryStream ms = new MemoryStream(fileBytes))
|
|
using (BinaryReader br = new BinaryReader(ms))
|
|
{
|
|
// 1. Verify the 10-Byte Header
|
|
// Signature is "ZXTape!" followed by EOF (0x1A)
|
|
byte[] signature = br.ReadBytes(8);
|
|
string sigString = System.Text.Encoding.ASCII.GetString(signature);
|
|
|
|
if (sigString != "ZXTape!\x1A")
|
|
throw new Exception("Invalid TZX Signature! Is this a real TZX file?");
|
|
|
|
byte majorVersion = br.ReadByte();
|
|
byte minorVersion = br.ReadByte();
|
|
|
|
// 2. Read the blocks until we hit the end of the file
|
|
while (ms.Position < ms.Length)
|
|
{
|
|
byte blockId = br.ReadByte();
|
|
|
|
switch (blockId)
|
|
{
|
|
case 0x10: // Standard Speed Data
|
|
var stdBlock = new StandardSpeedBlock
|
|
{
|
|
PauseAfterMs = br.ReadUInt16(),
|
|
DataLength = br.ReadUInt16()
|
|
};
|
|
stdBlock.Data = br.ReadBytes(stdBlock.DataLength);
|
|
blocks.Add(stdBlock);
|
|
break;
|
|
|
|
// TODO: Add cases for 0x11 (Turbo), 0x12 (Tone), 0x13 (Pulses), etc.
|
|
|
|
default:
|
|
throw new NotImplementedException($"TZX Block ID 0x{blockId:X2} is not supported yet!");
|
|
}
|
|
}
|
|
}
|
|
|
|
return blocks;
|
|
}
|
|
}
|
|
} |