Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added buttons to select display type #5

Merged
merged 2 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,19 @@
</article>
</div>
</div>
<div class="tile is-parent">
<article class="tile is-child">
<label>Display Settings</label>
<div class="tabs is-toggle">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be better stylistically as a bulma select?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this and I think it looks better as a toggle switch actually, but you are welcome to switch it to the select if you think it is better

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let's keep it as a toggle switch for now.

<ul>
<li id="hex-opt" class="is-active"><a onclick="driver.setRegMemDisplay('hex')">Hex</a></li>
<li id="dec-opt"><a onclick="driver.setRegMemDisplay('dec')">Decimal</a></li>
<li id="unsign-opt"><a onclick="driver.setRegMemDisplay('unsign')">Unsigned</a></li>
<li id="ascii-opt"><a onclick="driver.setRegMemDisplay('ascii')">ASCII</a></li>
</ul>
</div>
</article>
</div>
</section>
<script type="text/javascript" src="js/codemirror.js"></script>
<script type="text/javascript" src="venus.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/venus/glue/Driver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,8 @@ import kotlin.browser.window
}
Renderer.updateRegister(id, sim.getReg(id))
}

@JsName("setRegMemDisplay") fun setRegMemDisplay(dis_type: String) {
Renderer.setRegMemDisplay(dis_type)
}
}
84 changes: 82 additions & 2 deletions src/main/kotlin/venus/glue/Renderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ internal object Renderer {
private var activeMemoryAddress: Int = 0
/** The simulator being rendered */
private lateinit var sim: Simulator
/* The way the information in the registers is displayed*/
private var displayType = "hex"

/**
* Shows the simulator tab and hides other tabs
Expand Down Expand Up @@ -166,7 +168,13 @@ internal object Renderer {
*/
fun updateRegister(id: Int, value: Int, setActive: Boolean = false) {
val register = getElement("reg-$id-val") as HTMLInputElement
register.value = toHex(value)
register.value = when (displayType) {
"hex" -> toHex(value)
"dec" -> value.toString()
"unsign" -> toUnsigned(value)
"ascii" -> toAscii(value)
else -> toHex(value)
}
if (setActive) {
activeRegister?.classList?.remove("is-modified")
register.classList.add("is-modified")
Expand Down Expand Up @@ -328,7 +336,13 @@ internal object Renderer {
tdAddress.innerText = toHex(rowAddr)
for (i in 1..4) {
val tdByte = row.childNodes[i] as HTMLTableCellElement
tdByte.innerText = byteToHex(sim.loadByte(rowAddr + i - 1))
tdByte.innerText = when (displayType) {
"hex" -> byteToHex(sim.loadByte(rowAddr + i - 1))
"dec" -> byteToDec(sim.loadByte(rowAddr + i - 1))
"unsign" -> byteToUnsign(sim.loadByte(rowAddr + i - 1))
"ascii" -> byteToAscii(sim.loadByte(rowAddr + i - 1))
else -> byteToHex(sim.loadByte(rowAddr + i - 1))
}
}
} else {
tdAddress.innerText = "----------"
Expand Down Expand Up @@ -357,6 +371,18 @@ internal object Renderer {
return "$leftNibble$rightNibble"
}

private fun byteToAscii(b: Int): String {
return b.toChar().toString()
}

private fun byteToDec(b: Int): String {
return b.toByte().toString()
}

private fun byteToUnsign(b: Int): String {
return b.toString()
}

/**
* Converts a value to a two's complement hex number.
*
Expand Down Expand Up @@ -389,4 +415,58 @@ internal object Renderer {

return "0x" + suffix
}

private fun toUnsigned(value: Int): String {
var remainder = value.toLong()
var suffix = ""

// output as two's complement
if (remainder < 0) {
remainder += 0x1_0000_0000L
}

// convert to hex
while (remainder > 0) {
val hexDigit = hexMap[(remainder % 10).toInt()]
suffix = hexDigit + suffix
remainder /= 10
}

if (suffix.length < 1) {
suffix = "0"
}
return suffix
}

private fun toAscii(value: Int): String {
var remainder = value
var suffix = ""

while (remainder > 0) {
suffix = (remainder and 0xFFFF).toChar() + suffix
remainder = remainder ushr 8
}

return suffix
}

/**
* Sets the display type for all of the registers and memory
* Rerenders after
*/
fun setRegMemDisplay(dis_type: String) {
displayType = dis_type
optSetVisibily(dis_type)
updateAll()
}

private val opts = listOf("hex", "dec", "unsign", "ascii")

private fun optSetVisibily(opt: String) {
var tabDisplay: HTMLElement?
for (option in opts) {
tabDisplay = document.getElementById("$option-opt") as HTMLElement
tabDisplay.className = if (opt == option) "is-active" else ""
}
}
}