-
Notifications
You must be signed in to change notification settings - Fork 53
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,4 +155,8 @@ import kotlin.browser.window | |
} | ||
Renderer.updateRegister(id, sim.getReg(id)) | ||
} | ||
|
||
@JsName("setDisplay") fun render_setDisplay(dis_type: String) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to camelCase? Also maybe a better name would be |
||
Renderer.setDisplay(dis_type) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 display_type = "hex" | ||
|
||
/** | ||
* Shows the simulator tab and hides other tabs | ||
|
@@ -166,7 +168,17 @@ internal object Renderer { | |
*/ | ||
fun updateRegister(id: Int, value: Int, setActive: Boolean = false) { | ||
val register = getElement("reg-$id-val") as HTMLInputElement | ||
register.value = toHex(value) | ||
var shown_val: String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kotlin supports the
|
||
if (display_type.equals("hex")) { | ||
shown_val = toHex(value) | ||
} else if (display_type.equals("dec")) { | ||
shown_val = value.toString() | ||
} else if (display_type.equals("unsign")) { | ||
shown_val = toUnsigned(value) | ||
} else { | ||
shown_val = toHex(value) | ||
} | ||
register.value = shown_val | ||
if (setActive) { | ||
activeRegister?.classList?.remove("is-modified") | ||
register.classList.add("is-modified") | ||
|
@@ -389,4 +401,51 @@ 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 | ||
} | ||
|
||
/** | ||
* Sets the display type for all of the registers and memory | ||
* Rerenders after | ||
*/ | ||
fun setDisplay(dis_type: String) { | ||
display_type = dis_type | ||
optSetVisibily(dis_type) | ||
updateAll() | ||
} | ||
|
||
private val opts = listOf("hex", "dec", "unsign") | ||
|
||
private fun optSetVisibily(opt: String) { | ||
var tabDisplay: HTMLElement? | ||
for (option in opts) { | ||
if (opt.equals(option)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need .equals for strings in Kotlin, just |
||
tabDisplay = document.getElementById("$option-opt") as HTMLElement | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this line out of the if statement? |
||
tabDisplay.className = "is-active" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could make this a single line using the
|
||
} else { | ||
tabDisplay = document.getElementById("$option-opt") as HTMLElement | ||
tabDisplay.className = "" | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.