Code cleanup

This commit is contained in:
Felipe Lima 2018-08-08 20:16:58 -07:00
parent 67bc8d3084
commit 6706d4e774
3 changed files with 20 additions and 23 deletions

View File

@ -7,7 +7,6 @@ import java.util.regex.Pattern
class Assembler(private var memory: Memory, private val symbols: Symbols) { class Assembler(private var memory: Memory, private val symbols: Symbols) {
var codeLen = 0 var codeLen = 0
val BOOTSTRAP_ADDRESS = 0x600
var defaultCodePC = BOOTSTRAP_ADDRESS var defaultCodePC = BOOTSTRAP_ADDRESS
private var labels = Labels(this, symbols) private var labels = Labels(this, symbols)
@ -35,7 +34,7 @@ class Assembler(private var memory: Memory, private val symbols: Symbols) {
sanitizedLines sanitizedLines
.map { pattern.matcher(it) } .map { pattern.matcher(it) }
.filter { it.find() } .filter { it.find() }
.forEach { symbols.put(it.group(1), sanitize(it.group(2))) } .forEach { symbols[it.group(1)] = sanitize(it.group(2)) }
return sanitizedLines.filterNot { pattern.matcher(it).find() } return sanitizedLines.filterNot { pattern.matcher(it).find() }
} }
@ -85,10 +84,10 @@ class Assembler(private var memory: Memory, private val symbols: Symbols) {
return true return true
} }
if (input.matches("^\\w+\\s+.*?$".toRegex())) { param = if (input.matches("^\\w+\\s+.*?$".toRegex())) {
param = input.replace("^\\w+\\s+(.*?)".toRegex(), "$1") input.replace("^\\w+\\s+(.*?)".toRegex(), "$1")
} else if (input.matches("^\\w+$".toRegex())) { } else if (input.matches("^\\w+$".toRegex())) {
param = "" ""
} else { } else {
return false return false
} }
@ -419,4 +418,8 @@ class Assembler(private var memory: Memory, private val symbols: Symbols) {
pushByte(opcode) pushByte(opcode)
return true return true
} }
companion object {
const val BOOTSTRAP_ADDRESS = 0x600
}
} }

View File

@ -10,7 +10,7 @@ import android.view.View
open class Display(context: Context, attrs: AttributeSet) : View(context, attrs) { open class Display(context: Context, attrs: AttributeSet) : View(context, attrs) {
private val numX = 32 private val numX = 32
private val numY = 32 private val numY = 32
private val matrix = Array(numX, { IntArray(numY) }) private val matrix = Array(numX) { IntArray(numY) }
private val palette = arrayOf( private val palette = arrayOf(
"#000000", "#ffffff", "#880000", "#aaffee", "#000000", "#ffffff", "#880000", "#aaffee",

View File

@ -13,41 +13,35 @@ import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toolbar.* import kotlinx.android.synthetic.main.toolbar.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private var emulator: Emulator? = null private lateinit var emulator: Emulator
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
val ab: ActionBar = supportActionBar!! val actionBar: ActionBar = supportActionBar!!
ab.setDisplayHomeAsUpEnabled(true) actionBar.setDisplayHomeAsUpEnabled(true)
fabRun.setOnClickListener { fabRun.setOnClickListener {
display_wrapper.visibility = View.VISIBLE display_wrapper.visibility = View.VISIBLE
emulator = Emulator(display) emulator = Emulator(display)
val emu: Emulator = emulator as Emulator emulator.assembler.assembleCode(txtInstructions.text.toString().split("\n"))
emu.assembler.assembleCode(txtInstructions.text.toString().split("\n"))
Snackbar.make(layout_content, Snackbar.make(layout_content,
"Code assembled successfully, ${emu.assembler.codeLen} bytes.", "Code assembled successfully, ${emulator.assembler.codeLen} bytes.",
Snackbar.LENGTH_SHORT).show() Snackbar.LENGTH_SHORT).show()
emu.cpu.run() emulator.cpu.run()
} }
btnReset.setOnClickListener { btnReset.setOnClickListener {
val emu: Emulator = emulator as Emulator emulator.reset()
emu.reset()
} }
val onClickButton = { code: Int -> val onClickButton = { code: Int ->
if (emulator != null) { emulator.cpu.memory.storeKeypress(code)
val emu = emulator as Emulator
emu.cpu.memory.storeKeypress(code)
}
} }
arrowLeft.setOnClickListener { onClickButton(0x61) } arrowLeft.setOnClickListener { onClickButton(0x61) }
arrowRight.setOnClickListener { onClickButton(0x64) } arrowRight.setOnClickListener { onClickButton(0x64) }
arrowUp.setOnClickListener { onClickButton(0x77) } arrowUp.setOnClickListener { onClickButton(0x77) }
arrowDown.setOnClickListener { onClickButton(0x73) } arrowDown.setOnClickListener { onClickButton(0x73) }
} }
override fun onCreateOptionsMenu(menu: Menu?): Boolean { override fun onCreateOptionsMenu(menu: Menu?): Boolean {