Skip to content

Commit

Permalink
#17 Added ICartridge and implemented the interface in CrtFile
Browse files Browse the repository at this point in the history
  • Loading branch information
hagronnestad committed Apr 15, 2020
1 parent c8a7a79 commit d61e478
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
32 changes: 30 additions & 2 deletions ComputerSystems/Commodore64/Cartridge/FileFormats/Crt/CrtFile.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using System.Collections.Generic;
using Memory;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Commodore64.Cartridge.FileFormats.Crt {
public class CrtFile {
public class CrtFile : ICartridge {

public CrtHeader Header { get; set; }
public List<CrtChip> Chips { get; set; } = new List<CrtChip>();

public event EventHandler<MemoryReadEventArgs<byte>> OnRead;
public event EventHandler<MemoryWriteEventArgs<byte>> OnWrite;

public static CrtFile FromBytes(byte[] data) {
var crt = new CrtFile {
Expand All @@ -27,5 +32,28 @@ public static CrtFile FromBytes(byte[] data) {
public static CrtFile FromFile(string path) {
return FromBytes(File.ReadAllBytes(path));
}


// ICartridge implementation

public byte this[int i] {
get => Read(i);
set => Write(i, value);
}

public string Id => Header?.Identifier;
public bool ControlLineExRom => Header?.ExRomLine == 1;
public bool ControlLineGame => Header?.GameLine == 1;
public string Name => Header.Name;
public bool IsReadOnly => true;

public byte Read(int address) {
var chip = Chips.FirstOrDefault(x => address >= x.Address && address <= (x.Address + x.Length));
return chip.Data[address - chip.Address];
}

public void Write(int address, byte value) {
throw new NotImplementedException();
}
}
}
12 changes: 12 additions & 0 deletions ComputerSystems/Commodore64/Cartridge/ICartridge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Hardware.Memory;

namespace Commodore64.Cartridge {
public interface ICartridge : IMemory<byte> {

string Id { get; }
bool ControlLineExRom { get; }
bool ControlLineGame { get; }
string Name { get; }

}
}
1 change: 1 addition & 0 deletions ComputerSystems/Commodore64/Commodore64.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Cartridge\FileFormats\Crt\CrtFile.cs" />
<Compile Include="Cartridge\FileFormats\Crt\CrtHardwareType.cs" />
<Compile Include="Cartridge\FileFormats\Crt\CrtHeader.cs" />
<Compile Include="Cartridge\ICartridge.cs" />
<Compile Include="Cia\Cia2.cs" />
<Compile Include="Cia\Enums\Register.cs" />
<Compile Include="Colors.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Hardware/Memory/IMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IMemory<TValue> {
event EventHandler<MemoryReadEventArgs<TValue>> OnRead;
event EventHandler<MemoryWriteEventArgs<TValue>> OnWrite;

bool IsReadOnly { get; set; }
bool IsReadOnly { get; }

TValue Read(int address);
void Write(int address, TValue value);
Expand Down

0 comments on commit d61e478

Please sign in to comment.