Skip to content

Commit

Permalink
JS ALU override feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lvandeve committed Aug 5, 2021
1 parent ddfc5b3 commit bd0bb49
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
14 changes: 7 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ <h2>Supported Components</h2>
</body>
</html>
<!--the version cgi params are to encourage browsers to update js file cache when new version uploaded, by changing it to new date-->
<script src="editor.js?v=20210123"></script>
<script src="logicemu.js?v=20210123"></script>
<script src="main.js?v=20210123"></script>
<script src="circuits_help.js?v=20210123"></script>
<script src="circuits_articles.js?v=20210123"></script>
<script src="circuits_main.js?v=20210123"></script>
<script src="footer.js?v=20210123"></script>
<script src="editor.js?v=20210805"></script>
<script src="logicemu.js?v=20210805"></script>
<script src="main.js?v=20210805"></script>
<script src="circuits_help.js?v=20210805"></script>
<script src="circuits_articles.js?v=20210805"></script>
<script src="circuits_main.js?v=20210805"></script>
<script src="footer.js?v=20210805"></script>

54 changes: 52 additions & 2 deletions logicemu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4132,6 +4132,48 @@ function Mux() {
};
}

var alu_override_ops = {};

/*
registerAluOverride: JS-console only feature: override any numeric ALU's functionality with a custom JavaScript function.

How to use this:

For example for the following circuit, which has an ALU with id 0, the id to use for the override below:

llllllll
^^^^^^^^
########U0
^^^^^^^^
ssssssss

Then in the JS console, register a custom function for 0 as follows:

This example gives an alternating bit pattern, xored with the input:

registerAluOverride(0, function(inputs, outputs) {
//console.log('input: ' + inputs);
//console.log('output: ' + outputs);
for(var i = 0; i < outputs.length; i++) outputs[i] = !!((i & 1) ^ inputs[i]);
});

Implement any custom JS code in the function instead.

Your function receives 'inputs' and 'outputs' arrays. They are arrays of booleans whose size matches those of the current ALU.

You can read from inputs, and you can assign to outputs. The output has all false by default. Any true you fill in, will translate to the matching output wire being enabled.

These registered overrides are presistent until browser refresh, that is, they will stay even when loading different circuits, but are lost when refreshing the browser.

Saving the JS override function in the circuit (when exporting it, etc...) is not supported, there is no way to store the JS code in the circuits' ASCII representation. Save your custom registerAluOverride elsewhere as JS code instead.

To remove an override, do registerOverride(id, undefined)
*/
function registerAluOverride(id, fun) {
alu_override_ops[id] = fun;
update();
}

// Arithmetic Logic Unit ('U')
function Alu() {
this.parent = null;
Expand Down Expand Up @@ -4419,12 +4461,20 @@ function Alu() {
op -= 128;
}

if(floating) {
if(alu_override_ops[op]) {
this.updateOverride(inputs, op);
} else if(floating) {
this.updateFloating(inputs, op);
} else {
this.updateInteger(inputs, op, signed);
}
}
};

this.updateOverride = function(inputs, op) {
var o = alu_override_ops[op];
for(var i = 0; i < this.output.length; i++) this.output[i] = false;
o(inputs, this.output);
};

// first inputs are the data, last the select
// can handle both signed and unsigned integer case
Expand Down

0 comments on commit bd0bb49

Please sign in to comment.