conversions - Create `conversions.js` and move `hex2num` et al. out of `logging.js`

This commit is contained in:
n loewen 2023-08-29 20:30:44 -04:00
parent e9d721042b
commit 8bb3b9b43f
4 changed files with 6 additions and 51 deletions

View File

@ -2,7 +2,8 @@
const fs = require('fs');
const { logMemory, num2hex } = require('./logging.js');
const { logMemory } = require('./logging.js');
const { num2hex } = require('./conversions.js');
const DBG = require('./dbg.js');
const CFG = require('./machine.config.js');

View File

@ -1,7 +1,7 @@
#!/usr/bin/env node
const DBG = require('./dbg.js');
const { num2hex, bool2bit } = require('./logging.js');
const { num2hex, bool2bit } = require('./conversions.js');
const CFG = require('./machine.config.js');
const CPU = require('./cpu.js');
@ -16,7 +16,6 @@ main();
async function main() {
const input = await readPipedStdin();
dbg.nit('input: \n', input);
const debuggable = true; // FIXME - Get this as a command line flag instead
// if debuggable === true, the input is JSON {sourceInfo, machineCode}

View File

@ -1,4 +1,4 @@
const { num2hex } = require('./logging.js');
const { num2hex } = require('./conversions.js');
module.exports = class CPU {

View File

@ -1,3 +1,5 @@
let { num2hex } = require('./conversions.js');
/**
* Display a table of memory locations.
* Call with [start] and [end] indices to display a range.
@ -38,54 +40,7 @@ const logRunningHeader = () => {
console.log( `└─────────────────────┘`);
}
/**
* @param {number} num
* @returns {string}
*/
const num2hex = (num) => num.toString(16).toUpperCase().padStart(2, "0");
/**
* @param {string} hex
* @returns {number}
*/
const hex2num = (hex) => parseInt(hex, 16);
/**
* Convert a number to binary, padded to 8 bits
* See here for an explanation: https://stackoverflow.com/questions/9939760/how-do-i-convert-an-integer-to-binary-in-javascript
* @param {number} num
* @returns {string} binary representation of the input
**/
const num2bin = (num) => (num >>> 0).toString(2).padStart(8, "0");
/**
* Convert a number to binary, padded to 4 bits
* See here for an explanation: https://stackoverflow.com/questions/9939760/how-do-i-convert-an-integer-to-binary-in-javascript
* @param {number} num
* @returns {string} binary representation of the input
**/
const num2bin_4bit = (num) => (num >>> 0).toString(2).padStart(4, "0");
/**
* @param {string} bin
* @returns {number}
*/
const bin2num = (bin) => parseInt(bin, 2)
/**
* @param {Boolean} bool
* @returns {0|1}
**/
const bool2bit = (bool) => bool ? 1 : 0;
module.exports = {
"logMemory": logMemory,
"logRunningHeader": logRunningHeader,
"num2hex": num2hex,
"hex2num": hex2num,
"num2bin": num2bin,
"num2bin_4bit": num2bin_4bit,
"bin2num": bin2num,
"bool2bit": bool2bit,
}