Changed toStrBase() to accept a number of bits rather than a number of bytes, because bytes aren't always 8 bits (see PDP-10)

This commit is contained in:
Jeff Parsons 2017-02-24 10:58:56 -08:00 committed by Jeff Parsons
commit c0de732f76
14 changed files with 260 additions and 239 deletions

View file

@ -549,7 +549,7 @@ class DebuggerPDP10 extends Debugger {
*
* @this {DebuggerPDP10}
* @param {number|null|undefined} [off]
* @return {string} the hex representation of off
* @return {string} default base representation of off
*/
toStrOffset(off)
{
@ -561,13 +561,29 @@ class DebuggerPDP10 extends Debugger {
*
* @this {DebuggerPDP10}
* @param {DbgAddrPDP10} dbgAddr
* @return {string} the hex representation of the address
* @return {string} default base representation of the address
*/
toStrAddr(dbgAddr)
{
return this.toStrOffset(dbgAddr.addr);
}
/**
* toStrWord(w)
*
* @this {DebuggerPDP10}
* @param {number} w (up to, but not including, DATA_LIMIT)
* @return {string} octal representation of the 36-bit word, as two 18-bit values
*/
toStrWord(w)
{
/*
* ADDR_LIMIT is not derived from DATA_LIMIT; we're just taking advantage of the fact
* that ADDR_LIMIT happens to be exactly half of DATA_LIMIT, and they are both powers of two.
*/
return Str.toOct(w / PDP10.ADDR_LIMIT, 6) + ' ' + Str.toOct(w % PDP10.ADDR_LIMIT, 6);
}
/**
* dumpBlocks(aBlocks, sAddr)
*
@ -1759,7 +1775,7 @@ class DebuggerPDP10 extends Debugger {
{
var opNames = DebuggerPDP10.OPNAMES;
var dbgAddrOp = this.newAddr(dbgAddr.addr);
var opCode = this.getWord(dbgAddr, 2);
var opCode = this.getWord(dbgAddr, 1);
var opDesc;
for (var mask in this.opTable) {
@ -1783,7 +1799,7 @@ class DebuggerPDP10 extends Debugger {
var cOperands = opDesc.length - 1;
if (!opNum && !cOperands) {
sOperands = this.toStrBase(opCode);
sOperands = this.toStrWord(opCode);
}
for (var iOperand = 1; iOperand <= cOperands; iOperand++) {
@ -1815,12 +1831,13 @@ class DebuggerPDP10 extends Debugger {
var sLine = this.toStrAddr(dbgAddrOp) + ":";
if (dbgAddrOp.addr !== PDP10.ADDR_INVALID && dbgAddr.addr !== PDP10.ADDR_INVALID) {
do {
sOpCodes += ' ' + this.toStrBase(this.getWord(dbgAddrOp, 2));
var w = this.getWord(dbgAddrOp, 1);
sOpCodes += ' ' + this.toStrWord(w);
if (dbgAddrOp.addr == null) break;
} while (dbgAddrOp.addr != dbgAddr.addr);
}
sLine += Str.pad(sOpCodes, 24);
sLine += Str.pad(sOpCodes, 16);
sLine += Str.pad(sOpName, 5);
if (sOperands) sLine += ' ' + sOperands;
@ -2450,9 +2467,9 @@ class DebuggerPDP10 extends Debugger {
var nBase = this.nBase;
if (dbgAddr.nBase) this.nBase = dbgAddr.nBase;
var nBitsPerWord = 36;
var size = (sCmd == "db"? 1 : 2);
var nWords = len || 32;
var nWordsPerLine = 4; // fJSON? 16 : this.nBase;
var nWordsPerLine = (size == 1? 2 : 4);
var nLines = (((nWords + nWordsPerLine - 1) / nWordsPerLine)|0) || 1;
var sDump = "";
@ -2464,23 +2481,22 @@ class DebuggerPDP10 extends Debugger {
var w = this.getWord(dbgAddr, 1);
if (fJSON) {
if (sData) sData += ",";
sData += "0x"+ Str.toHex(w, nBitsPerWord >> 2);
sData += w;
} else {
sData += this.toStrBase(w, nBitsPerWord >> 3);
sData += ' ';
sData += this.toStrWord(w);
sData += ' ';
}
var nBytesPerWord = nBitsPerWord >> 3;
while (nBytesPerWord--) {
var c = w % 256;
sChars += (c >= 32 && c < 128? String.fromCharCode(c) : '.');
w /= 256;
for (var i = 0; size == 1 && i < 6; i++) {
var c = ((w % 64)|0) + 33;
sChars += String.fromCharCode(c);
w /= 64;
}
}
if (sDump) sDump += "\n";
if (fJSON) {
sDump += sData + ",";
} else {
sDump += sAddr + ": " + sData + ((n == 0)? (' ' + sChars) : "");
sDump += sAddr + ": " + sData + ((n < 0)? (' ' + sChars) : "");
}
}
@ -2498,13 +2514,10 @@ class DebuggerPDP10 extends Debugger {
*/
doEdit(asArgs)
{
var size, mask;
var fnGet, fnSet;
var sCmd = asArgs[0];
var sAddr = asArgs[1];
if (sCmd == "e" || sCmd == "ew") {
size = 2;
mask = 0xffff;
fnGet = this.getWord;
fnSet = this.setWord;
} else {
@ -2523,12 +2536,9 @@ class DebuggerPDP10 extends Debugger {
this.println("unrecognized value: " + asArgs[i]);
break;
}
if (vNew & ~mask) {
this.println("warning: " + Str.toHex(vNew) + " exceeds " + size + "-byte value");
}
this.println("changing " + this.toStrAddr(dbgAddr) + (this.messageEnabled(MessagesPDP10.BUS)? "" : (" from " + this.toStrBase(fnGet.call(this, dbgAddr), size))) + " to " + this.toStrBase(vNew, size));
this.println("changing " + this.toStrAddr(dbgAddr) + " from " + this.toStrWord(fnGet.call(this, dbgAddr)) + " to " + this.toStrWord(vNew));
//noinspection JSUnresolvedFunction
fnSet.call(this, dbgAddr, vNew, size);
fnSet.call(this, dbgAddr, vNew, 1);
}
}

View file

@ -97,7 +97,7 @@ var PDP10 = {
*/
ADDR_INVALID: -1,
ADDR_LIMIT: Math.pow(2, 18),
DATA_INVALID: 0,
DATA_INVALID: -1,
DATA_LIMIT: Math.pow(2, 36),
/*
* Assorted common opcodes

View file

@ -406,7 +406,7 @@ class MemoryPDP10 {
this.dbg.printMessage("attempt to read invalid address " + this.dbg.toStrBase(addr), true);
}
this.bus.fault(addr);
return 0;
return PDP10.DATA_INVALID;
}
/**

View file

@ -1596,7 +1596,7 @@ class CPUStatePDP11 extends CPUPDP11 {
if (DEBUG && this.dbg) {
if (this.messageEnabled(MessagesPDP11.TRAP)) {
var sReason = reason < 0? PDP11.REASONS[-reason] : this.dbg.toStrBase(reason);
this.printMessage("trap to vector " + this.dbg.toStrBase(vector, 1) + " (" + sReason + ")", MessagesPDP11.TRAP, true);
this.printMessage("trap to vector " + this.dbg.toStrBase(vector, 8) + " (" + sReason + ")", MessagesPDP11.TRAP, true);
}
}

View file

@ -1212,7 +1212,7 @@ class DebuggerPDP11 extends Debugger {
if (trapStatus) {
var reason = trapStatus >> 8;
var sReason = reason < 0? PDP11.REASONS[-reason] : this.toStrBase(reason);
this.println("trapped to " + this.toStrBase(trapStatus & 0xff, 1) + " (" + sReason + ")");
this.println("trapped to " + this.toStrBase(trapStatus & 0xff, 8) + " (" + sReason + ")");
}
this.dbgAddrNextCode = this.newAddr(this.cpu.getPC());
@ -2073,15 +2073,15 @@ class DebuggerPDP11 extends Debugger {
}
else if (opTypeOther == DebuggerPDP11.OP_DSTNUM3) {
disp = (opCode & 0x07);
sOperand = this.toStrBase(disp, 1);
sOperand = this.toStrBase(disp, 3);
}
else if (opTypeOther == DebuggerPDP11.OP_DSTNUM6) {
disp = (opCode & 0x3f);
sOperand = this.toStrBase(disp, 1);
sOperand = this.toStrBase(disp, 6);
}
else if (opTypeOther == DebuggerPDP11.OP_DSTNUM8) {
disp = (opCode & 0xff);
sOperand = this.toStrBase(disp, 1);
sOperand = this.toStrBase(disp, 8);
}
else {
/*
@ -2123,7 +2123,7 @@ class DebuggerPDP11 extends Debugger {
* When using R7 (aka PC), POST-INCREMENT is known as IMMEDIATE
*/
wIndex = this.getWord(dbgAddr, 2);
sOperand = '#' + this.toStrBase(wIndex, 0, true);
sOperand = '#' + this.toStrBase(wIndex, -1);
}
break;
@ -2135,7 +2135,7 @@ class DebuggerPDP11 extends Debugger {
* When using R7 (aka PC), POST-INCREMENT DEFERRED is known as ABSOLUTE
*/
wIndex = this.getWord(dbgAddr, 2);
sOperand = "@#" + this.toStrBase(wIndex, 0, true);
sOperand = "@#" + this.toStrBase(wIndex, -1);
sTarget = this.getTarget(wIndex);
}
break;
@ -2150,7 +2150,7 @@ class DebuggerPDP11 extends Debugger {
case PDP11.OPMODE.INDEX: // 0x6: INDEX
wIndex = this.getWord(dbgAddr, 2);
sOperand = this.toStrBase(wIndex, 0, true) + '(' + this.getRegName(reg) + ')';
sOperand = this.toStrBase(wIndex, -1) + '(' + this.getRegName(reg) + ')';
if (reg == 7) {
/*
* When using R7 (aka PC), INDEX is known as RELATIVE. However, instead of displaying
@ -2936,9 +2936,9 @@ class DebuggerPDP11 extends Debugger {
if (shift == size) {
if (fJSON) {
if (sData) sData += ",";
sData += "0x"+ Str.toHex(data, size * 2);
sData += "0x"+ Str.toHex(data, size << 1);
} else {
sData += this.toStrBase(data, size);
sData += this.toStrBase(data, size << 3);
sData += (size == 1? (i == 9? '-' : ' ') : " ");
}
data = shift = 0;
@ -3007,7 +3007,7 @@ class DebuggerPDP11 extends Debugger {
if (vNew & ~mask) {
this.println("warning: " + Str.toHex(vNew) + " exceeds " + size + "-byte value");
}
this.println("changing " + this.toStrAddr(dbgAddr) + (this.messageEnabled(MessagesPDP11.BUS)? "" : (" from " + this.toStrBase(fnGet.call(this, dbgAddr), size))) + " to " + this.toStrBase(vNew, size));
this.println("changing " + this.toStrAddr(dbgAddr) + (this.messageEnabled(MessagesPDP11.BUS)? "" : (" from " + this.toStrBase(fnGet.call(this, dbgAddr), size << 3))) + " to " + this.toStrBase(vNew, size << 3));
//noinspection JSUnresolvedFunction
fnSet.call(this, dbgAddr, vNew, size);
}

View file

@ -146,16 +146,16 @@ class DevicePDP11 extends Component {
if (DEBUGGER) {
var dbg = this.dbg;
if (sFilter && sName.indexOf(sFilter.toUpperCase()) < 0) return;
var nBits = 0;
var nRegs = 8;
var sDump = "";
var fIndex = false;
var nBytes = 0;
var nWidth = 8;
if (offset < 0) {
nBits = 22;
nRegs = aRegs.length;
offset = 0;
fIndex = true;
nBytes = 3;
nWidth = 4;
}
for (var i = 0; i < nRegs; i++) {
@ -163,7 +163,7 @@ class DevicePDP11 extends Component {
if (sDump) sDump += '\n';
sDump += sName + (fIndex? ('[' + Str.toDec(i, 2) + ']') : '') + ':';
}
sDump += ' ' + dbg.toStrBase(aRegs[offset + i], nBytes);
sDump += ' ' + dbg.toStrBase(aRegs[offset + i], nBits);
}
dbg.println(sDump + (fBreak? '\n' : ''));
}

View file

@ -647,31 +647,42 @@ class Debugger extends Component {
}
/**
* toStrBase(n, nBytes, fStripLeadingZeros)
* toStrBase(n, nBits)
*
* Use this instead of Str.toHex() or Str.toOct() to convert bytes/words to the Debugger's default base.
* Use this instead of Str's toOct()/toDec()/toHex() to convert numbers to the Debugger's default base.
*
* TODO: The 32-bit limitation on n is imposed by the Str functions we call, not us. Consider modifying
* those functions to support a higher number of bits (eg, 36), using arithmetic operators instead of bit-wise
* operators, and increasing the maximum number of supported bits to at least 52, while still using JavaScript
* floating-point numbers as the underlying data type.
*
* For now, the only component that really cares about supporting more than 32 bits (eg, 36 bits) is the PDP-10
* Debugger, which currently uses its own functions (eg, toStrWord()) to divide quantities into smaller (eg, 18-bit)
* values.
*
* @this {Debugger}
* @param {number|null|undefined} n
* @param {number} [nBytes] is the number of bytes to display, which we translate into a number of characters
* @param {boolean} [fStripLeadingZeros]
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [nBits] (-1 to strip leading zeros, 0 to allow a variable number of digits)
* @return {string}
*/
toStrBase(n, nBytes = 0, fStripLeadingZeros = false) {
toStrBase(n, nBits = 0) {
var s;
switch(this.nBase) {
case 8:
s = Str.toOct(n, nBytes * 3 /* - (nBytes > 2? 1 : 0) */);
s = Str.toOct(n, nBits > 0? ((nBits + 2)/3)|0 : 0);
break;
case 10:
s = n.toString();
/*
* The multiplier is actually Math.log(2)/Math.log(10), but an approximation is more than adequate.
*/
s = Str.toDec(n, nBits > 0? Math.ceil(nBits * 0.3) : 0);
break;
case 16:
default:
s = Str.toHex(n, nBytes * 2);
s = Str.toHex(n, nBits > 0? ((nBits + 3) >> 2) : 0);
break;
}
return (fStripLeadingZeros? Str.stripLeadingZeros(s) : s);
return (nBits < 0? Str.stripLeadingZeros(s) : s);
}
}

View file

@ -143,7 +143,7 @@ class Str {
*
* Converts an integer to binary, with the specified number of digits (up to the default of 32).
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cch] is the desired number of binary digits (32 is both the default and the maximum)
* @param {number} [grouping]
* @return {string} the binary representation of n
@ -171,7 +171,7 @@ class Str {
s = "," + s;
group = grouping;
}
s = (fInvalid ? '?' : ((n & 0x1) ? '1' : '0')) + s;
s = (fInvalid? '?' : ((n & 0x1)? '1' : '0')) + s;
n >>= 1;
group--;
}
@ -183,7 +183,7 @@ class Str {
*
* Converts an integer to binary, with the specified number of bytes (up to the default of 4).
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cb] is the desired number of binary bytes (4 is both the default and the maximum)
* @param {boolean} [fPrefix]
* @return {string} the binary representation of n
@ -197,7 +197,7 @@ class Str {
s = Str.toBin(n & 0xff, 8) + s;
n >>= 8;
}
return (fPrefix ? "0b" : "") + s;
return (fPrefix? "0b" : "") + s;
}
/**
@ -209,7 +209,7 @@ class Str {
* doesn't properly convert negative values. Moreover, if n is undefined, n.toString() will throw
* an exception, whereas this function will return '?' characters.
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cch] is the desired number of octal digits (0 or undefined for default of either 6 or 11)
* @param {boolean} [fPrefix]
* @return {string} the octal representation of n
@ -221,7 +221,7 @@ class Str {
if (cch) {
if (cch > 11) cch = 11;
} else {
cch = (n & ~0xffffff) ? 11 : ((n & ~0xffff) ? 8 : 6);
cch = (n & ~0xffffff)? 11 : ((n & ~0xffff)? 8 : 6);
}
/*
* An initial "falsey" check for null takes care of both null and undefined;
@ -240,7 +240,7 @@ class Str {
n >>= 3;
}
}
return (fPrefix ? "0o" : "") + s;
return (fPrefix? "0o" : "") + s;
}
/**
@ -252,7 +252,7 @@ class Str {
* doesn't properly convert negative values. Moreover, if n is undefined, n.toString() will throw
* an exception, whereas this function will return '?' characters.
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cch] is the desired number of decimal digits (0 or undefined for default of either 5 or 10)
* @return {string} the octal representation of n
*/
@ -263,7 +263,7 @@ class Str {
if (cch) {
if (cch > 10) cch = 10;
} else {
cch = (n & ~0xffff) ? 10 : 5;
cch = (n & ~0xffff)? 10 : 5;
}
/*
* An initial "falsey" check for null takes care of both null and undefined;
@ -302,7 +302,7 @@ class Str {
* s = "00000000".substr(0, 8 - s.length) + s;
* s = s.substr(0, cch).toUpperCase();
*
* @param {number|null|undefined} n is a 32-bit value
* @param {number|null|undefined} n (interpreted as a 32-bit value)
* @param {number} [cch] is the desired number of hex digits (0 or undefined for default of either 4 or 8)
* @param {boolean} [fPrefix]
* @return {string} the hex representation of n
@ -314,7 +314,7 @@ class Str {
if (cch) {
if (cch > 8) cch = 8;
} else {
cch = (n & ~0xffff) ? 8 : 4;
cch = (n & ~0xffff)? 8 : 4;
}
/*
* An initial "falsey" check for null takes care of both null and undefined;
@ -329,12 +329,12 @@ class Str {
} else {
while (cch-- > 0) {
var d = n & 0xf;
d += (d >= 0 && d <= 9 ? 0x30 : 0x41 - 10);
d += (d >= 0 && d <= 9? 0x30 : 0x41 - 10);
s = String.fromCharCode(d) + s;
n >>= 4;
}
}
return (fPrefix ? "0x" : "") + s;
return (fPrefix? "0x" : "") + s;
}
/**
@ -492,7 +492,7 @@ class Str {
* and the hyphen is last, you can avoid escaping those as well.
*/
k = k.replace(/([\\[\]*{}().+?])/g, "\\$1");
sMatch += (sMatch ? '|' : '') + k;
sMatch += (sMatch? '|' : '') + k;
}
return s.replace(new RegExp('(' + sMatch + ')', "g"), function(m)
{
@ -513,7 +513,7 @@ class Str {
static pad(s, cch, fPadLeft)
{
var sPadding = " ";
return fPadLeft ? (sPadding + s).slice(-cch) : (s + sPadding).slice(0, cch);
return fPadLeft? (sPadding + s).slice(-cch) : (s + sPadding).slice(0, cch);
}
/**