Reputation: 770
I'm currently implementing an emulator in Javascript, the memory model of the emulated platform is rather complex, so I'm having my reads and writes go via an array of functions, for example:
var MRead = [];
ReadBiosSpace = function(addr) { return (EnableBios ? BIOS[addr] : ROM[addr]); };
for (i = 0; i <= 0xFF; i++) { MRead[i] = ReadBiosSpace; };
function Read(addr) { return MRead[addr](addr); };
because obviously the Read and Write functions will be called extremely often (at least once per instruction, the main execution is Operators[Read(ProgramCounter)]()
) they are extremely performance sensitive.
Are there any performance optimizations that can be done? is this the best method?
Upvotes: 0
Views: 73
Reputation: 96810
In addition to the other answers, you can use this as well:
for (i = 0xFF - 1; i--) { MRead[i] = ReadBiosSpace; };
Upvotes: 1
Reputation: 339856
If EnableBios
doesn't change very often then the only obvious enhancement I can see is to have two different versions of ReadBiosSpace
and reassign the appropriate one to those first 256 locations each time it changes.
That'll avoid having to invoke the ternary operator each time one of those locations is accessed.
Upvotes: 1