diff --git a/compiler/examples/cube3d.p8 b/compiler/examples/cube3d.p8 index 6d1ad7a30..c0d868ad7 100644 --- a/compiler/examples/cube3d.p8 +++ b/compiler/examples/cube3d.p8 @@ -31,6 +31,7 @@ float[len(zcoor)] rotatedz sub start() { + set_irqvec() while true { if irq.time_changed { irq.time_changed = 0 diff --git a/compiler/src/prog8/ast/AST.kt b/compiler/src/prog8/ast/AST.kt index 0311411c4..1178513b0 100644 --- a/compiler/src/prog8/ast/AST.kt +++ b/compiler/src/prog8/ast/AST.kt @@ -420,8 +420,8 @@ interface INameScope { namespace.allLabelsAndVariables().forEach { println(" ".repeat(4 * (1 + indent)) + "${it.key} -> ${it.value::class.simpleName} at ${it.value.position}") } - namespace.statements.filter { it is INameScope }.forEach { - printNames(indent+1, it as INameScope) + namespace.statements.filterIsInstance().forEach { + printNames(indent+1, it) } } printNames(0, this) diff --git a/compiler/src/prog8/ast/AstChecker.kt b/compiler/src/prog8/ast/AstChecker.kt index bea8642a5..09752f586 100644 --- a/compiler/src/prog8/ast/AstChecker.kt +++ b/compiler/src/prog8/ast/AstChecker.kt @@ -61,7 +61,7 @@ class AstChecker(private val namespace: INameScope, override fun process(module: Module) { super.process(module) - val directives = module.statements.asSequence().filter { it is Directive }.groupBy { (it as Directive).directive } + val directives = module.statements.filterIsInstance().groupBy { it.directive } directives.filter { it.value.size > 1 }.forEach{ entry -> when(entry.key) { "%output", "%launcher", "%zeropage", "%address" -> diff --git a/compiler/src/prog8/ast/StmtReorderer.kt b/compiler/src/prog8/ast/StmtReorderer.kt index 03946afe6..8afdee781 100644 --- a/compiler/src/prog8/ast/StmtReorderer.kt +++ b/compiler/src/prog8/ast/StmtReorderer.kt @@ -28,7 +28,7 @@ class StatementReorderer(private val namespace: INameScope, private val heap: He module.statements.remove(mainBlock) module.statements.add(0, mainBlock) } - val varDecls = module.statements.filter { it is VarDecl } + val varDecls = module.statements.filterIsInstance() module.statements.removeAll(varDecls) module.statements.addAll(0, varDecls) val directives = module.statements.filter {it is Directive && it.directive in directivesToMove} @@ -44,7 +44,7 @@ class StatementReorderer(private val namespace: INameScope, private val heap: He } override fun process(block: Block): IStatement { - val subroutines = block.statements.asSequence().filter { it is Subroutine }.map { it as Subroutine }.toList() + val subroutines = block.statements.filterIsInstance() var numSubroutinesAtEnd = 0 // move all subroutines to the end of the block for (subroutine in subroutines) { @@ -91,7 +91,7 @@ class StatementReorderer(private val namespace: INameScope, private val heap: He override fun process(subroutine: Subroutine): IStatement { super.process(subroutine) - val varDecls = subroutine.statements.filter { it is VarDecl } + val varDecls = subroutine.statements.filterIsInstance() subroutine.statements.removeAll(varDecls) subroutine.statements.addAll(0, varDecls) val directives = subroutine.statements.filter {it is Directive && it.directive in directivesToMove} diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 4274b02a9..2eb4db690 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -160,7 +160,7 @@ private class StatementTranslator(private val prog: IntermediateProgram, } private fun processVariables(scope: INameScope) { - for(variable in scope.statements.asSequence().filter {it is VarDecl }.map { it as VarDecl }) + for(variable in scope.statements.filterIsInstance()) prog.variable(variable.scopedname, variable) for(subscope in scope.subScopes()) processVariables(subscope.value) diff --git a/compiler/src/prog8/stackvm/Memory.kt b/compiler/src/prog8/stackvm/Memory.kt index 720d89006..ef80064ba 100644 --- a/compiler/src/prog8/stackvm/Memory.kt +++ b/compiler/src/prog8/stackvm/Memory.kt @@ -94,4 +94,9 @@ class Memory { fun clear() { for(i in 0..65535) mem[i]=0 } -} \ No newline at end of file + + fun copy(from: Int, to: Int, numbytes: Int) { + for(i in 0 until numbytes) + mem[to+i] = mem[from+i] + } +} diff --git a/compiler/src/prog8/stackvm/StackVm.kt b/compiler/src/prog8/stackvm/StackVm.kt index 85d2ce869..b7a87487e 100644 --- a/compiler/src/prog8/stackvm/StackVm.kt +++ b/compiler/src/prog8/stackvm/StackVm.kt @@ -126,6 +126,7 @@ class StackVm(private var traceOutputFile: String?) { var callstack = MyStack() private set private var program = listOf() + private var labels = emptyMap() private var heap = HeapValues() private var traceOutput = if(traceOutputFile!=null) PrintStream(File(traceOutputFile), "utf-8") else null private var canvas: BitmapScreenPanel? = null @@ -136,8 +137,10 @@ class StackVm(private var traceOutputFile: String?) { var sourceLine: String = "" private set + fun load(program: Program, canvas: BitmapScreenPanel?) { this.program = program.program + this.labels = program.labels this.heap = program.heap this.canvas = canvas canvas?.requestFocusInWindow() @@ -158,7 +161,7 @@ class StackVm(private var traceOutputFile: String?) { P_irqd = false sourceLine = "" currentIns = this.program[0] - irqStartInstruction = program.labels["irq.irq"] + irqStartInstruction = null } fun step(instructionCount: Int = 5000) { @@ -1613,9 +1616,18 @@ class StackVm(private var traceOutputFile: String?) { val value = heap.get(iterable.heapId) evalstack.push(Value(DataType.UBYTE, if (value.array!!.all { v -> v != 0 }) 1 else 0)) } - Syscall.FUNC_SET_IRQVEC, Syscall.FUNC_SET_IRQVEC_EXCL -> TODO() - Syscall.FUNC_RESTORE_IRQVEC -> TODO() - Syscall.FUNC_MEMCOPY -> TODO() + Syscall.FUNC_SET_IRQVEC, Syscall.FUNC_SET_IRQVEC_EXCL -> { + irqStartInstruction = labels["irq.irq"] + } + Syscall.FUNC_RESTORE_IRQVEC -> { + irqStartInstruction = null + } + Syscall.FUNC_MEMCOPY -> { + val numbytes = evalstack.pop().integerValue() + val to = evalstack.pop().integerValue() + val from = evalstack.pop().integerValue() + mem.copy(from, to, numbytes) + } } }