aboutsummaryrefslogtreecommitdiffgithub
path: root/packages/novice/novice/util.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/novice/novice/util.ts')
-rw-r--r--packages/novice/novice/util.ts24
1 files changed, 17 insertions, 7 deletions
diff --git a/packages/novice/novice/util.ts b/packages/novice/novice/util.ts
index b069fc2..8a8dc6f 100644
--- a/packages/novice/novice/util.ts
+++ b/packages/novice/novice/util.ts
@@ -1,14 +1,14 @@
-function maskTo(val: number, bits: number): number {
+export function maskTo(val: number, bits: number): number {
return val & ((bits === 32) ? -1 : ~(-1 << bits));
}
-function sextTo(val: number, bits: number): number {
+export function sextTo(val: number, bits: number): number {
return (bits === 32 || (val & (1 << (bits - 1))) === 0)
? val
: val | (-1 << bits);
}
-function maxUnsignedVal(bits: number): number {
+export function maxUnsignedVal(bits: number): number {
// Need to do this instead of using bitwise operators to get around
// the fact that js bitwise operators work on 32-bit two's complement ints
return Math.pow(2, bits) - 1;
@@ -17,11 +17,11 @@ function maxUnsignedVal(bits: number): number {
// 0x80000001 & 0xffffffff = -0x7fffffff in js. This is pretty confusing
// behavior when dealing with 32-bit addresses, so get the expected
// value by clearing the MSB and then adding 2^31 to set it again.
-function forceUnsigned(val: number, bits: number): number {
+export function forceUnsigned(val: number, bits: number): number {
return (bits < 32 || !(val & (1 << 31))) ? val : (val & ~(1 << 31)) + Math.pow(2, 31);
}
-function padStr(str: string, to: number, withChar: string, right?: boolean) {
+export function padStr(str: string, to: number, withChar: string, right?: boolean) {
const need = to - str.length;
for (let i = 0; i < need; i++) {
str = right ? str + withChar : withChar + str;
@@ -29,9 +29,19 @@ function padStr(str: string, to: number, withChar: string, right?: boolean) {
return str;
}
-function fmtHex(val: number, bits: number): string {
+export function fmtHex(val: number, bits: number): string {
return '0x' + padStr(forceUnsigned(val, bits).toString(16),
Math.ceil(bits / 4), '0');
}
-export { forceUnsigned, maskTo, maxUnsignedVal, sextTo, padStr, fmtHex };
+export function fmtBin(val: number, bits: number): string {
+ return '0b' + padStr(forceUnsigned(val, bits).toString(2), bits, '0');
+}
+
+export function fmtBinOrHex(val: number, bits: number): string {
+ return (bits <= 4) ? fmtBin(val, bits) : fmtHex(val, bits);
+}
+
+export function range(n: number): number[] {
+ return [...new Array(n).keys()];
+}