My work on a 6502 emulator is continuing. I’ve run into some quirks with the embedded processor emulation wherein I need to load a 6502 binary file in an array starting at 0x1000.
Of course, loading a file is easy. But the offset part is isn’t. In C, I can do it easily.
Here’s the code for loading a file I’m using right now:
def loadbinary(filename)
@prog = File.open(filen, "rb") { |io| io.read }
@imagesize = @prog.size
end
If the load is loaded at 0x1000, the preceding space can be empty (before 0x1000).
Any ideas?
For anyone interesting, this is for the 6502.rb project I have on GitHub
Thanks.
You’ll probably need to allocate an array of
nbytes, wherenis the size of addressable RAM you want to emulate. From there you can start storing your data read in from the file into “RAM”.This is what I’d start with:
Note: You can’t use
String.sizeto get the length of the string, because it allows for multibyte characters. Instead, to get the number of bytes, you needbytesize, which, according to the documentation:Similarly, we can’t try to convert
@proginto an array usingsplit('')because that allows for multibyte characters too. Instead, we can get the bytes and turn that into an array. It could be done usingunpackorbytes.to_a.