<commit_before>// lc2.js - a simulator of the LC-2 processor in Javascript
//
// Author:  <NAME>
// License: ISC
// Website: http://spratt.github.io/lc2.js/

var LC2 = (function(LC2, undefined) {
	// constants
	var BASE = 2;
	var BITS = 16;
	var OPCODE_BITS = 4;
	var PAGE_BITS = 7;
	var PAGE_LOCS = BITS - PAGE_BITS;
	var REGISTERS = 8;

	var COND_NEG  = 1 << 0; // 2^0 = 1
	var COND_POS  = 1 << 1; // 2^1 = 2
	var COND_ZERO = 1 << 2; // 2^2 = 4

	// helpers
	var ones = function(n) {
		// Generate the largest unsigned integer representable in n bits
		var value = 0;
		for(var i = 0; i < n; ++i) {
			value = value | (1 << i);
		}
		return value;
	}

	var toUnsignedInt = function(n, bits) {
		// this trashes the sign bit which is fine for unsigned integers
		return n & ones(bits);
	};

	var toSignedInt = function(n, bits) {
		// Bitwise operators in Javascript coerce the number to a 32 bit integer
		// so this removes the value of the upper 32-n bits to coerce to n bits
		// while maintaining the sign bit
		var shift = 32 - bits;
		return (n << shift) >> shift;
	};
	
	var set_conditions = function(lc2_inst, value) {
		// set the condition bits on the given lc2 whose last result was value
		if(value < 0)
			lc2_inst.conds = COND_NEG;
		else if(value > 0)
			lc2_inst.conds = COND_POS;
		else
			lc2_inst.conds = COND_ZERO;
		lc2_inst.log("conds set to " + lc2_inst.conds);
	};

	var toBinaryString = function(num) {
		if(typeof(num) !== "number") return;
		return (num >> 0).toString(2);
	};

	// classes
	var Register = function(_id, _val) {
		if(!_id)  var _id  = "reg";
		if(!_val) var _val = 0;

		this.__defineGetter__("val", function() {
			return _val;
		});
		
		this.__defineSetter__("val", function(val) {
			// use the least signficant bits, and only as many as the CPU has
			_val = toSignedInt(val, BITS);
		});

		this.toString = function() {
			return "" + _id + "[" + _val + "]";
		};
	};

	var MemoryUnit = function(lc2_inst) {
		var mar = new Register("mar");
		var mdr = new Register("mdr");
		var pages = [];

		this.log = function() {};
		if(lc2_inst && typeof(lc2_inst.log) === 'function') {
			this.log = function(o) {
				lc2_inst.log(o);
			};
		}
		
		// fake initialize memory
		for(var page = 0; page < Math.pow(BASE, PAGE_BITS); ++page) {
			pages[page] = null;
		}

		
		this.__defineGetter__("mar", function() {
			return mar;
		});

		this.__defineGetter__("mdr", function() {
			return mdr;
		});<commit_msg>Now using typed arrays for registers<commit_after>// lc2.js - a simulator of the LC-2 processor in Javascript
//
// Author:  <NAME>
// License: ISC
// Website: http://spratt.github.io/lc2.js/

var LC2 = (function(LC2, undefined) {
	// constants
	var BASE = 2;
	var BITS = 16;
	var OPCODE_BITS = 4;
	var PAGE_BITS = 7;
	var PAGE_LOCS = BITS - PAGE_BITS;
	var REGISTERS = 8;

	var COND_NEG  = 1 << 0; // 2^0 = 1
	var COND_POS  = 1 << 1; // 2^1 = 2
	var COND_ZERO = 1 << 2; // 2^2 = 4

	// helpers
	var ones = function(n) {
		// Generate the largest unsigned integer representable in n bits
		var value = 0;
		for(var i = 0; i < n; ++i) {
			value = value | (1 << i);
		}
		return value;
	}

	var toSignedInt = function(n, bits) {
		// Bitwise operators in Javascript coerce the number to a 32 bit integer
		// so this removes the value of the upper 32-n bits to coerce to n bits
		// while maintaining the sign bit
		var shift = 32 - bits;
		return (n << shift) >> shift;
	};
	
	var set_conditions = function(lc2_inst, value) {
		// set the condition bits on the given lc2 whose last result was value
		if(value < 0)
			lc2_inst.conds = COND_NEG;
		else if(value > 0)
			lc2_inst.conds = COND_POS;
		else
			lc2_inst.conds = COND_ZERO;
		lc2_inst.log("conds set to " + lc2_inst.conds);
	};

	var toBinaryString = function(num) {
		if(typeof(num) !== "number") return;
		return (num >> 0).toString(2);
	};

	// classes
	var Register = function(_id) {
		if(!_id)  var _id  = "reg";
		var _val = new Int16Array(1);
		_val[0] = 0;

		this.__defineGetter__("val", function() {
			return _val[0];
		});
		
		this.__defineSetter__("val", function(val) {
			_val[0] = val;
		});

		this.toString = function() {
			return "" + _id + "[" + _val[0] + "]";
		};
	};

	var MemoryUnit = function(lc2_inst) {
		var mar = new Register("mar");
		var mdr = new Register("mdr");
		var pages = [];

		this.log = function() {};
		if(lc2_inst && typeof(lc2_inst.log) === 'function') {
			this.log = function(o) {
				lc2_inst.log(o);
			};
		}
		
		// fake initialize memory
		for(var page = 0; page < Math.pow(BASE, PAGE_BITS); ++page) {
			pages[page] = null;
		}

		
		this.__defineGetter__("mar", function() {
			return mar;
		});

		this.__defineGetter__("mdr", function() {
			return mdr;
		});
{'fraction_non_alphanumeric': 0.15155358275206088, 'fraction_numerical': 0.023462270133164237, 'mean_word_length': 2.872340425531915, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4947027', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1776, 'n_words': 516}