2019-09-20 20:12:58 +00:00
|
|
|
package razorvine.examplemachines
|
2019-09-15 03:04:57 +00:00
|
|
|
|
|
|
|
import kotlin.concurrent.scheduleAtFixedRate
|
|
|
|
import razorvine.ksim65.Bus
|
|
|
|
import razorvine.ksim65.Cpu6502
|
2019-09-19 19:29:33 +00:00
|
|
|
import razorvine.ksim65.IVirtualMachine
|
2019-09-15 03:04:57 +00:00
|
|
|
import razorvine.ksim65.Version
|
|
|
|
import razorvine.ksim65.components.*
|
|
|
|
import razorvine.ksim65.components.Timer
|
2019-09-18 00:00:29 +00:00
|
|
|
import javax.swing.ImageIcon
|
2019-09-15 03:04:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A virtual computer constructed from the various virtual components
|
|
|
|
*/
|
2019-09-19 19:29:33 +00:00
|
|
|
class VirtualMachine(title: String) : IVirtualMachine {
|
|
|
|
override val bus = Bus()
|
|
|
|
override val cpu = Cpu6502(false)
|
2019-09-15 03:04:57 +00:00
|
|
|
val ram = Ram(0x0000, 0xffff)
|
|
|
|
private val rtc = RealTimeClock(0xd100, 0xd108)
|
|
|
|
private val timer = Timer(0xd200, 0xd203, cpu)
|
|
|
|
|
|
|
|
private val debugWindow = DebugWindow(this)
|
2019-09-18 23:22:11 +00:00
|
|
|
private val hostDisplay = MainWindow(title)
|
2019-09-15 03:04:57 +00:00
|
|
|
private val display = Display(0xd000, 0xd00a, hostDisplay,
|
|
|
|
ScreenDefs.SCREEN_WIDTH_CHARS, ScreenDefs.SCREEN_HEIGHT_CHARS,
|
|
|
|
ScreenDefs.SCREEN_WIDTH, ScreenDefs.SCREEN_HEIGHT)
|
2019-09-18 18:57:46 +00:00
|
|
|
private val mouse = Mouse(0xd300, 0xd305, hostDisplay)
|
2019-09-15 03:04:57 +00:00
|
|
|
private val keyboard = Keyboard(0xd400, 0xd400, hostDisplay)
|
2019-09-20 20:12:58 +00:00
|
|
|
private var paused = false
|
2019-09-15 03:04:57 +00:00
|
|
|
|
|
|
|
init {
|
2019-09-18 00:00:29 +00:00
|
|
|
hostDisplay.iconImage = ImageIcon(javaClass.getResource("/icon.png")).image
|
|
|
|
debugWindow.iconImage = hostDisplay.iconImage
|
2019-09-19 19:29:33 +00:00
|
|
|
debugWindow.setLocation(hostDisplay.location.x+hostDisplay.width, hostDisplay.location.y)
|
2019-09-18 00:00:29 +00:00
|
|
|
|
2019-09-15 03:04:57 +00:00
|
|
|
ram[Cpu6502.RESET_vector] = 0x00
|
|
|
|
ram[Cpu6502.RESET_vector + 1] = 0x10
|
2019-09-16 21:52:25 +00:00
|
|
|
ram.loadPrg(javaClass.getResourceAsStream("/vmdemo.prg"))
|
2019-09-15 03:04:57 +00:00
|
|
|
|
|
|
|
bus += rtc
|
|
|
|
bus += timer
|
|
|
|
bus += display
|
|
|
|
bus += mouse
|
|
|
|
bus += keyboard
|
|
|
|
bus += ram
|
|
|
|
bus += cpu
|
|
|
|
bus.reset()
|
|
|
|
|
|
|
|
debugWindow.isVisible = true
|
2019-09-19 19:29:33 +00:00
|
|
|
hostDisplay.isVisible = true
|
|
|
|
hostDisplay.start()
|
2019-09-15 03:04:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 13:57:14 +00:00
|
|
|
override fun getZeroAndStackPages(): Array<UByte> = ram.getPages(0, 2)
|
|
|
|
|
2019-09-20 20:12:58 +00:00
|
|
|
override fun pause(paused: Boolean) {
|
|
|
|
this.paused = paused
|
|
|
|
}
|
2019-09-15 03:04:57 +00:00
|
|
|
|
2019-09-20 20:12:58 +00:00
|
|
|
override fun step() {
|
|
|
|
// step a full single instruction
|
2019-09-16 00:31:48 +00:00
|
|
|
while (cpu.instrCycles > 0) bus.clock()
|
|
|
|
bus.clock()
|
|
|
|
while (cpu.instrCycles > 0) bus.clock()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun start() {
|
2019-09-21 13:57:14 +00:00
|
|
|
javax.swing.Timer(10) {
|
|
|
|
debugWindow.updateCpu(cpu, bus)
|
|
|
|
}.start()
|
|
|
|
java.util.Timer("cpu-clock", true).scheduleAtFixedRate(1, 1) {
|
2019-09-16 00:31:48 +00:00
|
|
|
if(!paused) {
|
2019-09-18 23:22:11 +00:00
|
|
|
repeat(50) {
|
2019-09-20 20:12:58 +00:00
|
|
|
step()
|
2019-09-16 00:31:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-15 03:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun main(args: Array<String>) {
|
|
|
|
val machine = VirtualMachine("KSim65 demo virtual machine - using ksim65 v${Version.version}")
|
2019-09-16 00:31:48 +00:00
|
|
|
machine.start()
|
2019-09-15 03:04:57 +00:00
|
|
|
}
|