From 79efc4e8d3b05938dd1078878d6c8a71d4502c00 Mon Sep 17 00:00:00 2001 From: TQ Hirsch Date: Tue, 12 Sep 2023 19:30:36 +0200 Subject: [PATCH] Miscellaneous hacking --- src/main/kotlin/Main.kt | 9 ++++-- .../kotlin/com/thequux/mcpdp/RT11Loader.kt | 31 ++++++++++++++++--- .../com/thequux/mcpdp/peripheral/DL11.kt | 7 ++++- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 1f4ec73..744c6c5 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -7,16 +7,19 @@ import java.io.File fun main(args: Array) { val tb = org.jline.terminal.TerminalBuilder.terminal() + var mbus = MemBus(65536) + var cpu = CPU(mbus) + val console = DL11(tb.input(), tb.output()).apply { mount(mbus.unibus) } try { + tb.enterRawMode() - var mbus = MemBus(65536) - var cpu = CPU(mbus) - val console = DL11(tb.input(), tb.output()).apply { mount(mbus.unibus) } console.start() cpu.loadAbs(File(args[0])) cpu.runState = CPU.RunState.RUNNING cpu.run() } finally { + println("Exiting") + console.stop() } } \ No newline at end of file diff --git a/src/main/kotlin/com/thequux/mcpdp/RT11Loader.kt b/src/main/kotlin/com/thequux/mcpdp/RT11Loader.kt index 44c083d..9906d01 100644 --- a/src/main/kotlin/com/thequux/mcpdp/RT11Loader.kt +++ b/src/main/kotlin/com/thequux/mcpdp/RT11Loader.kt @@ -9,23 +9,40 @@ import java.io.File /// Loads an RT-11 object file into memory fun CPU.loadAbs(infile: File) { val core = this.core.modeSpace - val inStream = infile.inputStream().buffered() + val inStream = infile.inputStream() + var buf = ByteArray(6) var offset = 0 var addr: UShort = 0u var len: Int = 0 var cksum: Int = 0 + + var pos = 0 + + // Skip over leading zeros + + while (true) { + while(true) { + val b = inStream.read() + pos++ + when { + b < 0 -> throw Exception("EOF before data start") + b == 1 -> { inStream.skip(-1); pos--; break } + b == 0 -> continue + else -> throw Exception("Invalid first character") + } + } var read = inStream.read(buf, 0, 6) if (read == -1) { return } if (read < 6) { // TODO: report the error - throw Exception("Short record: $read bytes") + throw Exception("Short record: $read bytes at $pos") } if (buf[0] != 1.toByte() || buf[1] != 0.toByte()) { - throw Exception("Invalid block header") + throw Exception("Invalid block header at $pos: ${buf[0]},${buf[1]}") } len = buf[3].toInt().shl(8) + buf[2].toInt() - 6 addr = (buf[5].toUByte().toUShort().shl(8) + buf[4].toUByte().toUShort()).toUShort() @@ -42,9 +59,14 @@ fun CPU.loadAbs(infile: File) { } cksum = cksum and 0xFF // TODO: validate checksum == 0 + println("Loading 0x${len.toString(16)} bytes at 0x${addr.toString(16)}") if (len == 0) { // end of file - if (!(addr bit 0)) this.pc = addr + println("Tape ended at ${pos+len+7}") + if (!(addr bit 0)){ + this.pc = addr + println("Ready to run") + } return } else { // copy data to memory @@ -53,5 +75,6 @@ fun CPU.loadAbs(infile: File) { core.setb(addr++, buf[i].toUByte()) } } + pos += len+7 } } \ No newline at end of file diff --git a/src/main/kotlin/com/thequux/mcpdp/peripheral/DL11.kt b/src/main/kotlin/com/thequux/mcpdp/peripheral/DL11.kt index 6b08bbd..aae39c6 100644 --- a/src/main/kotlin/com/thequux/mcpdp/peripheral/DL11.kt +++ b/src/main/kotlin/com/thequux/mcpdp/peripheral/DL11.kt @@ -86,8 +86,13 @@ class DL11(private var istr: InputStream, private val ostr: OutputStream, val re } override fun start() { super.start() - reader = thread(block = this::readThread) + reader = thread(block = this::readThread, isDaemon = false) // reader?.interrupt() } + + override fun stop() { + super.stop() + reader?.interrupt() + } } \ No newline at end of file