2021-06-01 19:21:33 +00:00
|
|
|
package prog8tests
|
|
|
|
|
2021-07-11 17:04:53 +00:00
|
|
|
import org.junit.jupiter.api.Test
|
2021-10-10 22:22:04 +00:00
|
|
|
import org.junit.jupiter.api.TestInstance
|
2021-06-01 19:21:33 +00:00
|
|
|
import prog8.ast.Module
|
|
|
|
import prog8.ast.Program
|
|
|
|
import prog8.ast.base.DataType
|
|
|
|
import prog8.ast.base.Position
|
|
|
|
import prog8.ast.base.VarDeclType
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8.ast.expressions.ArrayIndexedExpression
|
|
|
|
import prog8.ast.expressions.IdentifierReference
|
|
|
|
import prog8.ast.expressions.NumericLiteralValue
|
|
|
|
import prog8.ast.expressions.PrefixExpression
|
2021-06-01 19:21:33 +00:00
|
|
|
import prog8.ast.statements.*
|
2021-10-19 20:36:05 +00:00
|
|
|
import prog8.compiler.astprocessing.isInRegularRAMof
|
2021-06-01 19:21:33 +00:00
|
|
|
import prog8.compiler.target.C64Target
|
2021-10-13 16:55:56 +00:00
|
|
|
import prog8.parser.SourceCode
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8tests.helpers.DummyFunctions
|
|
|
|
import prog8tests.helpers.DummyMemsizer
|
|
|
|
import kotlin.test.assertFalse
|
|
|
|
import kotlin.test.assertTrue
|
2021-06-01 19:21:33 +00:00
|
|
|
|
2021-07-11 17:04:53 +00:00
|
|
|
|
2021-07-11 16:18:27 +00:00
|
|
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
2021-06-01 19:21:33 +00:00
|
|
|
class TestMemory {
|
2021-10-10 22:22:04 +00:00
|
|
|
|
2021-06-01 19:21:33 +00:00
|
|
|
@Test
|
|
|
|
fun testInValidRamC64_memory_addresses() {
|
|
|
|
|
|
|
|
var memexpr = NumericLiteralValue.optimalInteger(0x0000, Position.DUMMY)
|
|
|
|
var target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
|
|
|
|
memexpr = NumericLiteralValue.optimalInteger(0x1000, Position.DUMMY)
|
|
|
|
target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
|
|
|
|
memexpr = NumericLiteralValue.optimalInteger(0x9fff, Position.DUMMY)
|
|
|
|
target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
|
|
|
|
memexpr = NumericLiteralValue.optimalInteger(0xc000, Position.DUMMY)
|
|
|
|
target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
|
|
|
|
memexpr = NumericLiteralValue.optimalInteger(0xcfff, Position.DUMMY)
|
|
|
|
target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testNotInValidRamC64_memory_addresses() {
|
|
|
|
|
|
|
|
var memexpr = NumericLiteralValue.optimalInteger(0xa000, Position.DUMMY)
|
|
|
|
var target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
|
|
|
|
memexpr = NumericLiteralValue.optimalInteger(0xafff, Position.DUMMY)
|
|
|
|
target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
|
|
|
|
memexpr = NumericLiteralValue.optimalInteger(0xd000, Position.DUMMY)
|
|
|
|
target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
|
|
|
|
memexpr = NumericLiteralValue.optimalInteger(0xffff, Position.DUMMY)
|
|
|
|
target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testInValidRamC64_memory_identifiers() {
|
2021-08-01 20:47:11 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer)
|
2021-10-10 23:34:55 +00:00
|
|
|
var target = createTestProgramForMemoryRefViaVar(program, 0x1000, VarDeclType.VAR)
|
2021-06-01 19:21:33 +00:00
|
|
|
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-10-10 23:34:55 +00:00
|
|
|
target = createTestProgramForMemoryRefViaVar(program, 0xd020, VarDeclType.VAR)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-10-10 23:34:55 +00:00
|
|
|
target = createTestProgramForMemoryRefViaVar(program, 0x1000, VarDeclType.CONST)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-10-10 23:34:55 +00:00
|
|
|
target = createTestProgramForMemoryRefViaVar(program, 0xd020, VarDeclType.CONST)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-10-10 23:34:55 +00:00
|
|
|
target = createTestProgramForMemoryRefViaVar(program, 0x1000, VarDeclType.MEMORY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 23:34:55 +00:00
|
|
|
private fun createTestProgramForMemoryRefViaVar(program: Program, address: Int, vartype: VarDeclType): AssignTarget {
|
2021-06-01 19:21:33 +00:00
|
|
|
val decl = VarDecl(vartype, DataType.BYTE, ZeropageWish.DONTCARE, null, "address", NumericLiteralValue.optimalInteger(address, Position.DUMMY), false, false, false, Position.DUMMY)
|
|
|
|
val memexpr = IdentifierReference(listOf("address"), Position.DUMMY)
|
|
|
|
val target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
|
|
|
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY)
|
2021-10-24 18:57:10 +00:00
|
|
|
val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY)
|
2021-10-14 21:56:23 +00:00
|
|
|
val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test"))
|
2021-10-19 20:30:30 +00:00
|
|
|
module.linkIntoProgram(program)
|
2021-06-01 19:21:33 +00:00
|
|
|
return target
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testInValidRamC64_memory_expression() {
|
|
|
|
val memexpr = PrefixExpression("+", NumericLiteralValue.optimalInteger(0x1000, Position.DUMMY), Position.DUMMY)
|
|
|
|
val target = AssignTarget(null, null, DirectMemoryWrite(memexpr, Position.DUMMY), Position.DUMMY)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testInValidRamC64_variable() {
|
|
|
|
val decl = VarDecl(VarDeclType.VAR, DataType.BYTE, ZeropageWish.DONTCARE, null, "address", null, false, false, false, Position.DUMMY)
|
|
|
|
val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY)
|
|
|
|
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY)
|
2021-10-24 18:57:10 +00:00
|
|
|
val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY)
|
2021-10-14 21:56:23 +00:00
|
|
|
val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test"))
|
2021-08-01 20:47:11 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer)
|
|
|
|
.addModule(module)
|
2021-10-19 20:30:30 +00:00
|
|
|
module.linkIntoProgram(program)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testInValidRamC64_memmap_variable() {
|
|
|
|
val address = 0x1000
|
|
|
|
val decl = VarDecl(VarDeclType.MEMORY, DataType.UBYTE, ZeropageWish.DONTCARE, null, "address", NumericLiteralValue.optimalInteger(address, Position.DUMMY), false, false, false, Position.DUMMY)
|
|
|
|
val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY)
|
|
|
|
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY)
|
2021-10-24 18:57:10 +00:00
|
|
|
val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY)
|
2021-10-14 21:56:23 +00:00
|
|
|
val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test"))
|
2021-08-01 20:47:11 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer)
|
|
|
|
.addModule(module)
|
2021-10-19 20:30:30 +00:00
|
|
|
module.linkIntoProgram(program)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testNotInValidRamC64_memmap_variable() {
|
|
|
|
val address = 0xd020
|
|
|
|
val decl = VarDecl(VarDeclType.MEMORY, DataType.UBYTE, ZeropageWish.DONTCARE, null, "address", NumericLiteralValue.optimalInteger(address, Position.DUMMY), false, false, false, Position.DUMMY)
|
|
|
|
val target = AssignTarget(IdentifierReference(listOf("address"), Position.DUMMY), null, null, Position.DUMMY)
|
|
|
|
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY)
|
2021-10-24 18:57:10 +00:00
|
|
|
val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY)
|
2021-10-14 21:56:23 +00:00
|
|
|
val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test"))
|
2021-08-01 20:47:11 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer)
|
|
|
|
.addModule(module)
|
2021-10-19 20:30:30 +00:00
|
|
|
module.linkIntoProgram(program)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testInValidRamC64_array() {
|
|
|
|
val decl = VarDecl(VarDeclType.VAR, DataType.ARRAY_UB, ZeropageWish.DONTCARE, null, "address", null, false, false, false, Position.DUMMY)
|
|
|
|
val arrayindexed = ArrayIndexedExpression(IdentifierReference(listOf("address"), Position.DUMMY), ArrayIndex(NumericLiteralValue.optimalInteger(1, Position.DUMMY), Position.DUMMY), Position.DUMMY)
|
|
|
|
val target = AssignTarget(null, arrayindexed, null, Position.DUMMY)
|
|
|
|
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY)
|
2021-10-24 18:57:10 +00:00
|
|
|
val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY)
|
2021-10-14 21:56:23 +00:00
|
|
|
val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test"))
|
2021-08-01 20:47:11 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer)
|
|
|
|
.addModule(module)
|
2021-10-19 20:30:30 +00:00
|
|
|
module.linkIntoProgram(program)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testInValidRamC64_array_memmapped() {
|
|
|
|
val address = 0x1000
|
|
|
|
val decl = VarDecl(VarDeclType.MEMORY, DataType.ARRAY_UB, ZeropageWish.DONTCARE, null, "address", NumericLiteralValue.optimalInteger(address, Position.DUMMY), false, false, false, Position.DUMMY)
|
|
|
|
val arrayindexed = ArrayIndexedExpression(IdentifierReference(listOf("address"), Position.DUMMY), ArrayIndex(NumericLiteralValue.optimalInteger(1, Position.DUMMY), Position.DUMMY), Position.DUMMY)
|
|
|
|
val target = AssignTarget(null, arrayindexed, null, Position.DUMMY)
|
|
|
|
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY)
|
2021-10-24 18:57:10 +00:00
|
|
|
val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY)
|
2021-10-14 21:56:23 +00:00
|
|
|
val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test"))
|
2021-08-01 20:47:11 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer)
|
|
|
|
.addModule(module)
|
2021-10-19 20:30:30 +00:00
|
|
|
module.linkIntoProgram(program)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertTrue(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testNotValidRamC64_array_memmapped() {
|
|
|
|
val address = 0xe000
|
|
|
|
val decl = VarDecl(VarDeclType.MEMORY, DataType.ARRAY_UB, ZeropageWish.DONTCARE, null, "address", NumericLiteralValue.optimalInteger(address, Position.DUMMY), false, false, false, Position.DUMMY)
|
|
|
|
val arrayindexed = ArrayIndexedExpression(IdentifierReference(listOf("address"), Position.DUMMY), ArrayIndex(NumericLiteralValue.optimalInteger(1, Position.DUMMY), Position.DUMMY), Position.DUMMY)
|
|
|
|
val target = AssignTarget(null, arrayindexed, null, Position.DUMMY)
|
|
|
|
val assignment = Assignment(target, NumericLiteralValue.optimalInteger(0, Position.DUMMY), Position.DUMMY)
|
2021-10-24 18:57:10 +00:00
|
|
|
val subroutine = Subroutine("test", mutableListOf(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, false, mutableListOf(decl, assignment), Position.DUMMY)
|
2021-10-14 21:56:23 +00:00
|
|
|
val module = Module(mutableListOf(subroutine), Position.DUMMY, SourceCode.Generated("test"))
|
2021-08-01 20:47:11 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer)
|
|
|
|
.addModule(module)
|
2021-10-19 20:30:30 +00:00
|
|
|
module.linkIntoProgram(program)
|
2021-10-19 20:36:05 +00:00
|
|
|
assertFalse(target.isInRegularRAMof(C64Target.machine))
|
2021-06-01 19:21:33 +00:00
|
|
|
}
|
|
|
|
}
|