Fantasy 8Bit system (F8), is a fantasy 8bit console and a set of libraries for creating fantasy 8bit consoles.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
937B

  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. }
  10. get size(){return (this.__map) ? this.__map.length : 0;}
  11. get address(){return this.__addr;}
  12. set address(a){
  13. if (this.__map)
  14. this.__addr = Math.min(this.__map.length, Math.max(0, a));
  15. }
  16. get byte(){return (this.__map) ? this.__map[this.__addr] : -1;}
  17. set byte(b){
  18. if (!this.__ro && this.__map)
  19. this.__map[this.__addr] = b;
  20. }
  21. sysStore(address, data){
  22. if (this.__map){
  23. let dc = data;
  24. if (address < 0 || address >= this.__map.length)
  25. throw new RangeError("Memory address out of range.");
  26. if (this.__map.length - address < dc.length)
  27. dc = dc.slice(0, this.__map.length - address);
  28. this.__map.set(address, dc);
  29. return dc.length;
  30. }
  31. }
  32. }
  33. module.exports = Memory;