Fantasy 8Bit system (F8), is a fantasy 8bit console and a set of libraries for creating fantasy 8bit consoles.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

59 lines
1.6KB

  1. class Memory{
  2. constructor(size, ro){
  3. this.__ro = (ro === true);
  4. this.__map = null;
  5. this.__addr = 0;
  6. if (size > 0){
  7. this.__map = new Uint8Array(size);
  8. }
  9. this.__listeners = {};
  10. }
  11. get size(){return (this.__map) ? this.__map.length : 0;}
  12. get address(){return this.__addr;}
  13. set address(a){
  14. if (this.__map)
  15. this.__addr = Math.min(this.__map.length, Math.max(0, a));
  16. }
  17. get byte(){return (this.__map) ? this.__map[this.__addr] : -1;}
  18. set byte(b){
  19. if (!this.__ro && this.__map){
  20. this.__map[this.__addr] = b;
  21. if (this.__addr in this.__listeners)
  22. this.__listeners[this.__addr].forEach((fn)=>{fn(b);});
  23. }
  24. }
  25. onAddressWrite(addr, fn){
  26. if (addr < 0 || addr >= this.size)
  27. throw new RangeError("Memory address is out of bounds.");
  28. if (typeof(fn) !== 'function')
  29. throw new TypeErrpr("Expected a callback function.");
  30. if (!(addr in this.__listeners))
  31. this.__listeners[addr] = [];
  32. // WARNING: Not testing to see if using the same function more than once.
  33. this.__listeners[addr].push(fn);
  34. }
  35. // This method is intended for the emulator to fill a Read-Only memory module with data prior to
  36. // the start of execution. This method should never be used in a running system.
  37. sysStore(address, data){
  38. if (this.__map){
  39. let dc = data;
  40. if (address < 0 || address >= this.__map.length)
  41. throw new RangeError("Memory address out of range.");
  42. if (this.__map.length - address < dc.length)
  43. dc = dc.slice(0, this.__map.length - address);
  44. this.__map.set(address, dc);
  45. return dc.length;
  46. }
  47. }
  48. }
  49. module.exports = Memory;