From 34dcce67e42fd7823d89469ad227d51812e012c7 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 10 Jul 2019 07:10:34 +0200 Subject: [PATCH] fixed petscii conversion when printing text --- .../src/prog8/compiler/target/c64/Petscii.kt | 33 +++++++++++++++++++ compiler/src/prog8/vm/astvm/AstVm.kt | 2 +- compiler/src/prog8/vm/astvm/ScreenDialog.kt | 32 +++++++++++------- compiler/src/prog8/vm/stackvm/StackVm.kt | 2 +- examples/test.p8 | 17 +++++----- 5 files changed, 63 insertions(+), 23 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/Petscii.kt b/compiler/src/prog8/compiler/target/c64/Petscii.kt index fcb0ff00f..a0a7a2c30 100644 --- a/compiler/src/prog8/compiler/target/c64/Petscii.kt +++ b/compiler/src/prog8/compiler/target/c64/Petscii.kt @@ -1086,5 +1086,38 @@ class Petscii { val decodeTable = if(lowercase) decodingScreencodeLowercase else decodingScreencodeUppercase return screencode.map { decodeTable[it.toInt()] }.joinToString("") } + + fun petscii2scr(petscii_code: Short, inverseVideo: Boolean): Short { + val code = when { + petscii_code <= 0x1f -> petscii_code + 128 + petscii_code <= 0x3f -> petscii_code.toInt() + petscii_code <= 0x5f -> petscii_code - 64 + petscii_code <= 0x7f -> petscii_code - 32 + petscii_code <= 0x9f -> petscii_code + 64 + petscii_code <= 0xbf -> petscii_code - 64 + petscii_code <= 0xfe -> petscii_code - 128 + petscii_code == 255.toShort() -> 95 + else -> throw CharConversionException("petscii code out of range") + } + if(inverseVideo) + return (code or 0x80).toShort() + return code.toShort() + } + + fun scr2petscii(screencode: Short): Short { + val petscii = when { + screencode <= 0x1f -> screencode + 64 + screencode <= 0x3f -> screencode.toInt() + screencode <= 0x5d -> screencode +123 + screencode == 0x5e.toShort() -> 255 + screencode == 0x5f.toShort() -> 223 + screencode <= 0x7f -> screencode + 64 + screencode <= 0xbf -> screencode - 128 + screencode <= 0xfe -> screencode - 64 + screencode == 255.toShort() -> 191 + else -> throw CharConversionException("screencode out of range") + } + return petscii.toShort() + } } } diff --git a/compiler/src/prog8/vm/astvm/AstVm.kt b/compiler/src/prog8/vm/astvm/AstVm.kt index 604488756..72f4db1f4 100644 --- a/compiler/src/prog8/vm/astvm/AstVm.kt +++ b/compiler/src/prog8/vm/astvm/AstVm.kt @@ -621,7 +621,7 @@ class AstVm(val program: Program) { dialog.canvas.setCursorPos(args[0].integerValue(), args[1].integerValue()) } "c64.CHROUT" -> { - dialog.canvas.printChar(args[0].byteval!!) + dialog.canvas.printPetscii(args[0].byteval!!) } "c64flt.print_f" -> { dialog.canvas.printText(args[0].floatval.toString(), 1, true) diff --git a/compiler/src/prog8/vm/astvm/ScreenDialog.kt b/compiler/src/prog8/vm/astvm/ScreenDialog.kt index 003940266..fb570bedb 100644 --- a/compiler/src/prog8/vm/astvm/ScreenDialog.kt +++ b/compiler/src/prog8/vm/astvm/ScreenDialog.kt @@ -61,23 +61,23 @@ class BitmapScreenPanel : KeyListener, JPanel() { g2d.color = Colors.palette[color % Colors.palette.size] g2d.drawLine(x1, y1, x2, y2) } - fun printText(text: String, color: Short, lowercase: Boolean) { + fun printText(text: String, color: Short, lowercase: Boolean, inverseVideo: Boolean=false) { val t2 = text.substringBefore(0.toChar()) val lines = t2.split('\n') for(line in lines.withIndex()) { - printTextSingleLine(line.value, color, lowercase) + printTextSingleLine(line.value, color, lowercase, inverseVideo) if(line.index=(SCREENWIDTH /8)) { cursorY++ @@ -86,12 +86,12 @@ class BitmapScreenPanel : KeyListener, JPanel() { } } - fun printChar(char: Short) { + fun printPetscii(char: Short, inverseVideo: Boolean=false) { if(char==13.toShort() || char==141.toShort()) { cursorX=0 cursorY++ } else { - setChar(cursorX, cursorY, char, 1) + setPetscii(cursorX, cursorY, char, 1, inverseVideo) cursorX++ if (cursorX >= (SCREENWIDTH / 8)) { cursorY++ @@ -100,10 +100,18 @@ class BitmapScreenPanel : KeyListener, JPanel() { } } - fun setChar(x: Int, y: Int, screenCode: Short, color: Short) { + fun setPetscii(x: Int, y: Int, petscii: Short, color: Short, inverseVideo: Boolean) { g2d.clearRect(8*x, 8*y, 8, 8) val colorIdx = (color % Colors.palette.size).toShort() - val coloredImage = Charset.getColoredChar(screenCode, colorIdx) + val screencode = Petscii.petscii2scr(petscii, inverseVideo) + val coloredImage = Charset.getColoredChar(screencode, colorIdx) + g2d.drawImage(coloredImage, 8*x, 8*y , null) + } + + fun setChar(x: Int, y: Int, screencode: Short, color: Short) { + g2d.clearRect(8*x, 8*y, 8, 8) + val colorIdx = (color % Colors.palette.size).toShort() + val coloredImage = Charset.getColoredChar(screencode, colorIdx) g2d.drawImage(coloredImage, 8*x, 8*y , null) } @@ -116,16 +124,16 @@ class BitmapScreenPanel : KeyListener, JPanel() { return Pair(cursorX, cursorY) } - fun writeText(x: Int, y: Int, text: String, color: Short, lowercase: Boolean) { + fun writeText(x: Int, y: Int, text: String, color: Short, lowercase: Boolean, inverseVideo: Boolean=false) { val colorIdx = (color % Colors.palette.size).toShort() var xx=x for(clearx in xx until xx+text.length) { g2d.clearRect(8*clearx, 8*y, 8, 8) } - for(sc in Petscii.encodeScreencode(text, lowercase)) { + for(sc in Petscii.encodePetscii(text, lowercase)) { if(sc==0.toShort()) break - setChar(xx++, y, sc, colorIdx) + setPetscii(xx++, y, sc, colorIdx, inverseVideo) } } diff --git a/compiler/src/prog8/vm/stackvm/StackVm.kt b/compiler/src/prog8/vm/stackvm/StackVm.kt index 04ec2383e..c33675629 100644 --- a/compiler/src/prog8/vm/stackvm/StackVm.kt +++ b/compiler/src/prog8/vm/stackvm/StackVm.kt @@ -1926,7 +1926,7 @@ class StackVm(private var traceOutputFile: String?) { } "c64.CHROUT" -> { val sc=variables.getValue("A").integerValue() - canvas?.printChar(sc.toShort()) + canvas?.printPetscii(sc.toShort()) callstack.pop() } "c64.GETIN" -> { diff --git a/examples/test.p8 b/examples/test.p8 index 71d281d13..d6516b9cb 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,18 +1,17 @@ %import c64utils %zeropage basicsafe -%option enable_floats ~ main { sub start() { - - word zc - word qq = zc>>13 - ubyte[] colors = [1,2,3,4,5,6,7,8] - - uword bb = zc>>13 - c64.SPCOL[0] = colors[(zc>>13) as byte + 4] - + c64scr.print("you start here! --> S\n") + for A in 0 to 16 { + for Y in 0 to 39 { + if rnd() >128 c64.CHROUT(109) + else c64.CHROUT(110) + } + } + c64scr.print(" x <-- try to find your way here!") } }