You are reading a single comment by @DrAzzy and its replies. Click here to read the full conversation.
  • Say you want to save memory storing functions (particularly verbose ones). I plan to do this for answering HTTP requests, since that's something that can involve a considerable amount of difficult-to-shrink code (the strings that need to be returned, mostly), yet is something that likely is not used all that often, so it can afford to drag a little bit.

    Let's start by adding this function to our eeprom object:

    I2C1.setup({scl:B8,sda:B9});
    rom=require("AT24").connect(I2C1,128,512);
    
    rom.r = function (id) {
    	var x=this.read(1024+id*4,4);
    	if (x[2]!=255) {return eval(this.reads((x[1]+(x[0]<<8)),(x[3]+(x[2]<<8))));}
    };
    

    Here, starting at 0x0400, we'll start storing a table of indices for functions stored on the eeprom, 4 bytes per (2 each for starting address, and length). We'll need some helper functions to load the EEPROM, and see what's on it. Setting all 4 bytes of the index entry for a function marks that function ID as unused, though we look at the third byte, because if that's 255, the result is unambiguously invalid.

    
    maxid=255;
    ftst=1024;
    
    //getList() returns an object containing one property for each function listed in the function index on the rom, and also prints out it's progress in human readable format to assist the operator in loading the rom.
    
    function getList() {
    	var count=0;
    	var map={};
    	for (var i=0;i<maxid;i++) {
    		var b=rom.read(ftst+i*4,4);
    		if (b[2]!=255) { //if b[2]==255, length ~= 64K, which is clearly not valid data. 
    			count++;
    			var a=b[1]+(b[0]<<8);
    			var l=b[3]+(b[2]<<8);
    			map[i]=[a,l];
    			console.log(i+". 0x"+a.toString(16)+" "+l+" bytes."); 
    		}
    	}
    	console.log("Scan complete "+ count+ " functions in index"); 
    	return map;
    }
    
    //getFunction(id) will return a string containing the code for that function, assuming it exists. 
    
    function getFunction(id) {
    	var x=rom.read(ftst+id*4,4);
    	if (x[2]!=255) {return rom.reads((x[1]+(x[0]<<8)),(x[3]+(x[2]<<8)));}
    }
    
    //isSafe(address, length, map) takes a 'map' object (as returned by getList()), and returns 1 if a function of specified length can be placed in the specified address without overwriting something. 
    //If it fails, print the ID of the function that it's got a problem with. 
    
    function isSafe(adr,len,map) {
    	var max=len+adr;
    	for (var i=0;i<maxid;i++) {
    		if (map[i]!=undefined) {
    			var a=map[i][0];
    			var l=map[i][1];
    			if (((a < adr)&&(a+l > adr))||((a > adr)&&(a < max))) {
    				console.log("Conflict on function "+i);
    				return 0;
    			}
    		}
    	}
    	return 1;
    }
    
    //addFunction(id, address, function) - This creates a new entry for a function of 'id', located at 'address' in the function index, and writes that and the function (supplied as a string) to the rom, provided that that can be done without overwriting another function.
    
    
    function addFunction(id,adr,str) {
    	console.log("Adding function of length "+str.length+" at address "+adr+" with ID: "+id);
    	if (isSafe(adr,str.length,getList())) {
    		rom.writel(adr,str);
    		var tarr=new Uint8Array(4);
    		tarr[0]=(adr>>8);
    		tarr[1]=(adr&0xFF);
    		tarr[2]=(str.length>>8);
    		tarr[3]=(str.length&0xFF);
    		rom.writeb(ftst+4*id,tarr);
    	} else {
    		console.log("Selected location would overlap with other function!");
    	}
    }
    
    //deleteFunction(id) deletes the function with that ID from the function index. 
    
    function deleteFunction(id) {
    	console.log("deleting function: " + id);
    	rom.writes(ftst+4*id,"\xFF\xFF\xFF\xFF");
    }
    
    //cleanup - remove all the stuff related to this. 
    function cleanup() {
      delete getFunction;
      delete isSafe;
      delete deleteFunction;
      delete getList;
      delete addFunction;
      delete maxid;
      delete getFunction;
      delete ftst;
      delete cleanup;
    }
    
    

    And of course, the first function we add can be this one:

    
    addFunction(0,2048,'ftst=1024;maxid=255;function getList(){for(var a=0,d={},c=0;c<maxid;c++){var b=rom.read(ftst+4*c,4);if(255!=b[2]){a++;var e=b[1]+(b[0]<<8),b=b[3]+(b[2]<<8);d[c]=[e,b];console.log(c+". 0x"+e.toString(16)+" "+b+" bytes.")}}console.log(a+" functions in index");return d}function getFunction(a){a=rom.read(ftst+4*a,4);if (a[2]!=255) {return rom.reads(a[1]+(a[0]<<8),a[3]+(a[2]<<8))}function isSafe(a,d,c){d+=a;for(var b=0;b<maxid;b++)if(void 0!=c[b]){var e=c[b][0],f=c[b][1];if(e<a&&e+f>a||e>a&&e<d)return console.log("Conflict on function "+b),0}return 1}function addFunction(a,d,c){console.log("Add fun of length "+c.length+" @  "+d.toString(16)+" ID: "+a);if(isSafe(d,c.length,getList())){rom.writel(d,c);var b=new Uint8Array(4);b[0]=d>>8;b[1]=d&255;b[2]=c.length>>8;b[3]=c.length&255;rom.writeb(ftst+4*a,b)}else console.log("Not enough space there!")}function deleteFunction(a){console.log("deleting function: "+a);rom.writes(ftst+4*a,"\xff\xff\xff\xff")} function cleanup() {delete getFunction;delete isSafe;delete deleteFunction;delete getList;delete addFunction;delete maxid;delete getFunction;delete ftst;delete cleanup;}');
    
    

    Length is like 1160 bytes.

About

Avatar for DrAzzy @DrAzzy started