I'm trying to write a chip8 emulator. I'm at the step where I load the rom into the memory. According to the documentation each instruction is 2 bytes and max memory addressed is 4K. So I define the memory as an array of ushorts.
struct Chip8
{
ushort[4096] memory;
...
To load in the rom I do this
void read(string rom)
{
import std.file : exists, getSize, read;
if (exists(rom))
{
writeln("Loading the Rom");
auto romSize = getSize(rom);
writeln("The Rom size is : ", romSize);
if (romSize > this.memory.length - memStart)
writefln("Rom Size is too big! romSize = %s MemSize = %s", romSize,
this.memory.length - memStart);
else
{
// is it possible to to!int[] or do I have to use a cast here?
this.memory[memStart..memStart + romSize] = cast(ushort[])read(rom);
}
}
else
{
writeln("Cannot read ", rom);
}
}
But I get a range violation error.
core.exception.RangeError@source\chip.d(85): Range violation
I don't understand why? According to Windows the file is 478 bytes. memStart is 0x200. 0x200 + 478 = 990 which is well within the 4096Kb array which I created.