2019-02-04 23:52:21 +00:00
|
|
|
|
// <copyright file="Rom.cs" company="Adrian Conlon">
|
|
|
|
|
// Copyright (c) Adrian Conlon. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace EightBit
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
public class Rom : Memory
|
|
|
|
|
{
|
|
|
|
|
private byte[] bytes;
|
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
public Rom(int size) => this.bytes = new byte[size];
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
public Rom()
|
|
|
|
|
: this(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
public override int Size => this.bytes.Length;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-18 00:52:45 +00:00
|
|
|
|
public static int Load(Stream file, ref byte[] output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1)
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
var size = (int)file.Length;
|
|
|
|
|
|
|
|
|
|
if ((maximumSize > 0) && ((size - readOffset) > maximumSize))
|
2019-02-04 23:52:21 +00:00
|
|
|
|
{
|
2019-02-02 15:12:51 +00:00
|
|
|
|
throw new InvalidOperationException("File is too large");
|
2019-02-04 23:52:21 +00:00
|
|
|
|
}
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
if ((limit < 0) || (limit > size))
|
2019-02-04 23:52:21 +00:00
|
|
|
|
{
|
2019-02-02 15:12:51 +00:00
|
|
|
|
limit = (int)size;
|
2019-02-04 23:52:21 +00:00
|
|
|
|
}
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
var extent = limit + writeOffset;
|
|
|
|
|
if (output.Length < extent)
|
|
|
|
|
{
|
|
|
|
|
var updated = new byte[extent];
|
|
|
|
|
Array.Copy(output, updated, output.Length);
|
|
|
|
|
output = updated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.Seek(readOffset, SeekOrigin.Begin);
|
|
|
|
|
using (var reader = new BinaryReader(file))
|
|
|
|
|
{
|
2019-07-24 22:46:06 +00:00
|
|
|
|
reader.Read(output, writeOffset, extent);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Load(string path, ref byte[] output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1)
|
|
|
|
|
{
|
|
|
|
|
using (var file = File.Open(path, FileMode.Open))
|
|
|
|
|
{
|
|
|
|
|
return Load(file, ref output, writeOffset, readOffset, limit, maximumSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Load(FileStream file, int writeOffset = 0, int readOffset = 0, int limit = -1)
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
var maximumSize = this.Size - writeOffset;
|
|
|
|
|
return Load(file, ref this.Bytes(), writeOffset, readOffset, limit, maximumSize);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Load(string path, int writeOffset = 0, int readOffset = 0, int limit = -1)
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
var maximumSize = this.Size - writeOffset;
|
|
|
|
|
return Load(path, ref this.Bytes(), writeOffset, readOffset, limit, maximumSize);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Load(byte[] from, int writeOffset = 0, int readOffset = 0, int limit = -1)
|
|
|
|
|
{
|
|
|
|
|
if (limit < 0)
|
2019-02-04 23:52:21 +00:00
|
|
|
|
{
|
2019-05-01 21:47:40 +00:00
|
|
|
|
limit = Math.Min(from.Length, this.Size - readOffset);
|
2019-02-04 23:52:21 +00:00
|
|
|
|
}
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
var extent = limit + writeOffset;
|
2019-02-04 23:52:21 +00:00
|
|
|
|
if (this.Size < extent)
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
var updated = new byte[extent];
|
2019-02-04 23:52:21 +00:00
|
|
|
|
Array.Copy(this.Bytes(), updated, this.Size);
|
|
|
|
|
this.Bytes() = updated;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
Array.Copy(from, readOffset, this.Bytes(), writeOffset, limit);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
return limit;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
public override byte Peek(ushort address) => this.Bytes()[address];
|
|
|
|
|
|
|
|
|
|
protected ref byte[] Bytes() => ref this.bytes;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
protected override void Poke(ushort address, byte value) => this.Bytes()[address] = value;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|