diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index 89b32a171..998950456 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -468,9 +468,9 @@ internal class ProgramAndVarsGen( } } - private fun nonZpVariables2asm(variables2: List) { + private fun nonZpVariables2asm(variables: List) { asmgen.out("") - val (varsNoInit, varsWithInit) = variables2.partition { it.uninitialized } + val (varsNoInit, varsWithInit) = variables.partition { it.uninitialized } if(varsNoInit.isNotEmpty()) { asmgen.out("; non-zeropage variables without initialization value") asmgen.out(" .section BSS") diff --git a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt index 4f15ae969..27048e598 100644 --- a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt +++ b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt @@ -41,7 +41,6 @@ internal class BeforeAsmAstChanger(val program: Program, if(!options.reinitGlobals) { block.statements.asSequence().filterIsInstance().forEach { if(it.type==VarDeclType.VAR) { - it.zeropage = ZeropageWish.NOT_IN_ZEROPAGE it.findInitializer(program)?.let { initializer -> it.value = initializer.value // put the init value back into the vardecl } diff --git a/examples/test.p8 b/examples/test.p8 index 256b10302..59ced2ac0 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,7 +6,8 @@ main { uword b_wordvar - ubyte b_bb =123 + uword b_initwordvar = 12345 ; TODO FIX THIS INIT VALUE FOR noreinit=true + ubyte b_bb =123 ; TODO FIX THIS INIT VALUE FOR noreinit=true float b_fl ubyte[10] b_emptyarray ubyte[10] b_filledarray = [1,2,3,4,5,6,7,8,9,10] @@ -15,6 +16,7 @@ main { sub start() { uword wordvar + uword initwordvar = 12345 float fl ubyte bb =123 ubyte[10] emptyarray @@ -25,12 +27,14 @@ main { uword slab2 = memory("slab2",200, $1000) txt.print("**subroutine scope**\n") - txt.print("uninit wordvar=") + txt.print("init wordvar=") + txt.print_uw(initwordvar) + txt.print("\ninit bb=") + txt.print_ub(bb) + txt.print("\nuninit wordvar=") txt.print_uw(wordvar) txt.print("\nuninit float=") floats.print_f(fl) - txt.print("\ninit bb=") - txt.print_ub(bb) txt.print("\nuninit emptyarray[2]=") txt.print_ub(emptyarray[2]) txt.print("\nuninit wordarray[2]=") @@ -41,12 +45,14 @@ main { txt.print_ub(filledarray[2]) txt.print("\n**block scope**\n") - txt.print("uninit b_wordvar=") + txt.print("init wordvar=") + txt.print_uw(b_initwordvar) + txt.print("\ninit b_bb=") + txt.print_ub(b_bb) + txt.print("\nuninit b_wordvar=") txt.print_uw(b_wordvar) txt.print("\nuninit b_float=") floats.print_f(b_fl) - txt.print("\ninit b_bb=") - txt.print_ub(b_bb) txt.print("\nuninit b_emptyarray[2]=") txt.print_ub(b_emptyarray[2]) txt.print("\nuninit b_wordarray[2]=") diff --git a/intermediate/src/prog8/intermediate/IRFileWriter.kt b/intermediate/src/prog8/intermediate/IRFileWriter.kt index aa7240de9..4b0004ee2 100644 --- a/intermediate/src/prog8/intermediate/IRFileWriter.kt +++ b/intermediate/src/prog8/intermediate/IRFileWriter.kt @@ -23,7 +23,7 @@ class IRFileWriter(private val irProgram: IRProgram, outfileOverride: Path?) { out.write("\n\n") if(irProgram.options.reinitGlobals) { - // note: this a block of code that loads values and stores them into the global variables to reset their values. + // this a code that re-loads startup values into all global (block-level) variables. writeCodeChunk(irProgram.globalInits) } out.write("\n") diff --git a/virtualmachine/src/prog8/vm/SysCalls.kt b/virtualmachine/src/prog8/vm/SysCalls.kt index 4c711e08f..c6891a7b1 100644 --- a/virtualmachine/src/prog8/vm/SysCalls.kt +++ b/virtualmachine/src/prog8/vm/SysCalls.kt @@ -89,7 +89,7 @@ object SysCalls { when(call) { Syscall.RESET -> { - vm.reset() + vm.reset(false) } Syscall.EXIT ->{ vm.exit(vm.registers.getUB(SyscallRegisterBase).toInt()) diff --git a/virtualmachine/src/prog8/vm/VirtualMachine.kt b/virtualmachine/src/prog8/vm/VirtualMachine.kt index 30dc5f901..17bf4e273 100644 --- a/virtualmachine/src/prog8/vm/VirtualMachine.kt +++ b/virtualmachine/src/prog8/vm/VirtualMachine.kt @@ -37,7 +37,7 @@ class VirtualMachine(irProgram: IRProgram) { val callStack = Stack>() val valueStack = Stack() // max 128 entries var breakpointHandler: ((pcChunk: IRCodeChunk, pcIndex: Int) -> Unit)? = null // can set custom breakpoint handler - var pcChunk: IRCodeChunk + var pcChunk = IRCodeChunk(null, null) var pcIndex = 0 var stepCount = 0 var statusCarry = false @@ -51,7 +51,7 @@ class VirtualMachine(irProgram: IRProgram) { program = VmProgramLoader().load(irProgram, memory) require(irProgram.st.getAsmSymbols().isEmpty()) { "virtual machine can't yet process asmsymbols defined on command line" } cx16virtualregsBaseAddress = (irProgram.st.lookup("cx16.r0") as? StMemVar)?.address?.toInt() ?: 0xff02 - pcChunk = program.firstOrNull() ?: IRCodeChunk(null, null) + reset(false) } fun run() { @@ -81,13 +81,17 @@ class VirtualMachine(irProgram: IRProgram) { } } - fun reset() { + fun reset(clearMemory: Boolean) { + // "reset" the VM without erasing the currently loaded program + // this allows you to re-run the program multiple times without having to reload it registers.reset() - // memory.reset() + if(clearMemory) + memory.reset() pcIndex = 0 - pcChunk = IRCodeChunk(null, null) + pcChunk = program.firstOrNull() ?: IRCodeChunk(null, null) stepCount = 0 callStack.clear() + valueStack.clear() statusCarry = false statusNegative = false statusZero = false