Error
Decabytes
mycontributiontotheworld at gmail.com
Mon Oct 31 01:02:48 UTC 2022
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](http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#3.0) each instruction is 2 bytes and max memory addressed is 4K. So I define the memory as an array of ushorts.
```D
struct Chip8
{
ushort[4096] memory;
...
```
To load in the rom I do this
```D
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 at source\chip.d(85): Range violation`
I don't understand why? According to Windows [the file is 478
bytes](https://github.com/corax89/chip8-test-rom/blob/master/test_opcode.ch8). memStart is 0x200. 0x200 + 478 = 990 which is well within the 4096Kb array which I created.
More information about the Digitalmars-d-learn
mailing list