digitalmars.D.learn - Error
I'm trying to write a chip8 emulator. I'm at the step where I
load the rom into the memory. According to the
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 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.
Oct 30 2022
```d writeln (getSize(rom)) ``` reports 478 bytes but since you work with ushorts (why? as far as I can see, this is a 8 bit machine) you convert the read(rom) into ushorts, which is only half in size: ```d writeln (cast(ushort[])read(rom)); ``` gives you 478/2 = 239 bytes ```d this.memory[memStart..memStart + romSize] = cast(ushort[])read(rom); ``` fails because both ranges have different sizes.
Oct 31 2022








Johann <johann.lermer elvin.eu>