From d33aed4ed58cc882e144120b5ff761d52964b468 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 7 Feb 2024 22:36:43 +0100 Subject: [PATCH] added txt.petscii2scr() and txt.petscii2scr_str() --- compiler/res/prog8lib/atari/textio.p8 | 2 +- compiler/res/prog8lib/c128/textio.p8 | 1 + compiler/res/prog8lib/c64/floats.p8 | 2 +- compiler/res/prog8lib/c64/textio.p8 | 2 +- compiler/res/prog8lib/cx16/floats.p8 | 2 +- compiler/res/prog8lib/cx16/textio.p8 | 2 +- compiler/res/prog8lib/pet32/textio.p8 | 2 +- ...unctions.p8 => shared_floats_functions.p8} | 0 .../res/prog8lib/shared_textio_functions.p8 | 39 +++++++++++ compiler/res/prog8lib/virtual/textio.p8 | 15 ++++ .../TestImportedModulesOrderAndOptions.kt | 5 +- docs/source/libraries.rst | 1 + docs/source/todo.rst | 6 +- examples/test.p8 | 70 ++++++++----------- 14 files changed, 100 insertions(+), 49 deletions(-) rename compiler/res/prog8lib/{floats_functions.p8 => shared_floats_functions.p8} (100%) create mode 100644 compiler/res/prog8lib/shared_textio_functions.p8 diff --git a/compiler/res/prog8lib/atari/textio.p8 b/compiler/res/prog8lib/atari/textio.p8 index 536058a86..a6a5c7af9 100644 --- a/compiler/res/prog8lib/atari/textio.p8 +++ b/compiler/res/prog8lib/atari/textio.p8 @@ -3,7 +3,7 @@ %import syslib %import conv - +%import shared_textio_functions txt { diff --git a/compiler/res/prog8lib/c128/textio.p8 b/compiler/res/prog8lib/c128/textio.p8 index 511f4175c..340c29550 100644 --- a/compiler/res/prog8lib/c128/textio.p8 +++ b/compiler/res/prog8lib/c128/textio.p8 @@ -4,6 +4,7 @@ %import syslib %import conv +%import shared_textio_functions txt { diff --git a/compiler/res/prog8lib/c64/floats.p8 b/compiler/res/prog8lib/c64/floats.p8 index d5682e385..b33c5f4c4 100644 --- a/compiler/res/prog8lib/c64/floats.p8 +++ b/compiler/res/prog8lib/c64/floats.p8 @@ -1,7 +1,7 @@ ; Prog8 definitions for floating point handling on the Commodore-64 %option enable_floats, no_symbol_prefixing, ignore_unused -%import floats_functions +%import shared_floats_functions floats { ; ---- this block contains C-64 floating point related functions ---- diff --git a/compiler/res/prog8lib/c64/textio.p8 b/compiler/res/prog8lib/c64/textio.p8 index 29ca48509..aafcf7505 100644 --- a/compiler/res/prog8lib/c64/textio.p8 +++ b/compiler/res/prog8lib/c64/textio.p8 @@ -4,7 +4,7 @@ %import syslib %import conv - +%import shared_textio_functions txt { diff --git a/compiler/res/prog8lib/cx16/floats.p8 b/compiler/res/prog8lib/cx16/floats.p8 index 547626730..cee961814 100644 --- a/compiler/res/prog8lib/cx16/floats.p8 +++ b/compiler/res/prog8lib/cx16/floats.p8 @@ -1,7 +1,7 @@ ; Prog8 definitions for floating point handling on the CommanderX16 %option enable_floats, no_symbol_prefixing, ignore_unused -%import floats_functions +%import shared_floats_functions floats { ; ---- this block contains C-64 compatible floating point related functions ---- diff --git a/compiler/res/prog8lib/cx16/textio.p8 b/compiler/res/prog8lib/cx16/textio.p8 index d97cb8655..694f5b49c 100644 --- a/compiler/res/prog8lib/cx16/textio.p8 +++ b/compiler/res/prog8lib/cx16/textio.p8 @@ -4,7 +4,7 @@ %import syslib %import conv - +%import shared_textio_functions txt { diff --git a/compiler/res/prog8lib/pet32/textio.p8 b/compiler/res/prog8lib/pet32/textio.p8 index 544170577..5f1991f9c 100644 --- a/compiler/res/prog8lib/pet32/textio.p8 +++ b/compiler/res/prog8lib/pet32/textio.p8 @@ -4,7 +4,7 @@ %import syslib %import conv - +%import shared_textio_functions txt { %option no_symbol_prefixing, ignore_unused diff --git a/compiler/res/prog8lib/floats_functions.p8 b/compiler/res/prog8lib/shared_floats_functions.p8 similarity index 100% rename from compiler/res/prog8lib/floats_functions.p8 rename to compiler/res/prog8lib/shared_floats_functions.p8 diff --git a/compiler/res/prog8lib/shared_textio_functions.p8 b/compiler/res/prog8lib/shared_textio_functions.p8 new file mode 100644 index 000000000..1d7485bfc --- /dev/null +++ b/compiler/res/prog8lib/shared_textio_functions.p8 @@ -0,0 +1,39 @@ +txt { + ; the textio functions shared across compiler targets + %option merge, no_symbol_prefixing, ignore_unused + + asmsub petscii2scr(ubyte petscii_char @A) -> ubyte @A { + ; -- convert petscii character to screencode + %asm {{ + sta P8ZP_SCRATCH_REG + lsr a + lsr a + lsr a + lsr a + lsr a + tax + lda _offsets,x + eor P8ZP_SCRATCH_REG + rts +_offsets .byte 128, 0, 64, 32, 64, 192, 128, 128 + }} + } + + asmsub petscii2scr_str(str petscii_string @AY) { + ; -- convert petscii string to screencodes string + %asm {{ + sta P8ZP_SCRATCH_W1 + sty P8ZP_SCRATCH_W1+1 + ldy #0 +- lda (P8ZP_SCRATCH_W1),y + beq + + jsr petscii2scr + sta (P8ZP_SCRATCH_W1),y + iny + bne - ++ rts + }} +} + + +} diff --git a/compiler/res/prog8lib/virtual/textio.p8 b/compiler/res/prog8lib/virtual/textio.p8 index 7fb1aa9a6..84fb9dcb6 100644 --- a/compiler/res/prog8lib/virtual/textio.p8 +++ b/compiler/res/prog8lib/virtual/textio.p8 @@ -145,4 +145,19 @@ sub setchr (ubyte col, ubyte row, ubyte char) { txt.chrout(char) } +sub petscii2scr(ubyte petscii_char) -> ubyte { + ; -- convert petscii character to screencode + byte[8] offsets = [128, 0, 64, 32, 64, 192, 128, 128] + return petscii_char ^ offsets[petscii_char>>5] +} + +sub petscii2scr_str(str petscii_string) { + ; -- convert petscii string to screencodes string + cx16.r0 = petscii_string + while @(cx16.r0)!=0 { + @(cx16.r0) = petscii2scr(@(cx16.r0)) + cx16.r0++ + } +} + } diff --git a/compiler/test/TestImportedModulesOrderAndOptions.kt b/compiler/test/TestImportedModulesOrderAndOptions.kt index 3c894247c..457deb356 100644 --- a/compiler/test/TestImportedModulesOrderAndOptions.kt +++ b/compiler/test/TestImportedModulesOrderAndOptions.kt @@ -42,8 +42,9 @@ main { "textio", "syslib", "conv", + "shared_textio_functions", "floats", - "floats_functions", + "shared_floats_functions", "math", "prog8_lib" ) @@ -100,7 +101,7 @@ main { listOf( internedStringsModuleName, filenameBase, - "textio", "syslib", "conv", "floats", "floats_functions", "math", "prog8_lib" + "textio", "syslib", "conv", "shared_textio_functions", "floats", "shared_floats_functions", "math", "prog8_lib" ) } options.floats shouldBe true diff --git a/docs/source/libraries.rst b/docs/source/libraries.rst index 8e481691f..ef34a386c 100644 --- a/docs/source/libraries.rst +++ b/docs/source/libraries.rst @@ -177,6 +177,7 @@ dealing with text-based input and output (to the screen). Such as - filling or clearing the screen and colors - scrolling the text on the screen - placing individual characters on the screen +- convert petscii to screencode characters All routines work with Screencode character encoding, except `print`, `chrout` and `input_chars`, these work with PETSCII encoding instead. diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 870f3bacb..5d348898e 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,8 +1,9 @@ TODO ==== -&pointervar[x] isn't correct?? (at least in IR) - +&pointervar[x] isn't the correct value +&pointervar[x] AST doesn't print correctly +@(s) where s is a str parameter, doesn't work (after merge in boolean): move all "OperatorXinplace" from expressionGen to AssignmentGen, see if we can get rid of the Result return type. @@ -13,6 +14,7 @@ Future Things and Ideas ^^^^^^^^^^^^^^^^^^^^^^^ Compiler: +- can we support singed % (remainder) somehow? - instead of copy-pasting inline asmsubs, make them into a 64tass macro and use that instead. that will allow them to be reused from custom user written assembly code as well. - Multidimensional arrays and chained indexing, purely as syntactic sugar over regular arrays. diff --git a/examples/test.p8 b/examples/test.p8 index be3451504..ca4cb5cf1 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,50 +6,42 @@ main { sub start() { + str namestring = petscii:"The Quick Brown Fox Jumps Over The Lazy Dog\n0123456789!#$%^&*()-=_+[]{};:'<>,./?\n" + str namestring2 = petscii:"The Quick Brown Fox Jumps Over The Lazy Dog\n0123456789!#$%^&*()-=_+[]{};:'<>,./?\n" + txt.petscii2scr_str(namestring2) + for cx16.r0L in 0 to len(namestring) { + txt.print_ubhex(namestring[cx16.r0L], false) + txt.spc() + txt.print_ubhex(namestring2[cx16.r0L], false) + txt.nl() + } + txt.nl() + sys.exit(1) + str name1 = "" str name2 = "hello \r\n" str name3 = " \n\rhello" str name4 = " \n\r\xa0\xa0\xff\xffhello\x02\x02\x02 \n " - txt.print("strip:\n") - string.strip(name1) - txt.chrout('[') - txt.print(name1) - txt.print("]\n") - string.strip(name2) - txt.chrout('[') - txt.print(name2) - txt.print("]\n") - string.strip(name3) - txt.chrout('[') - txt.print(name3) - txt.print("]\n") - string.strip(name4) - txt.chrout('[') - txt.print(name4) - txt.print("]\n") + foo(name2) + } - str tname1 = "" - str tname2 = "hello \r\n" - str tname3 = " \n\r\x09hello" - str tname4 = " \n\x09\x0b\r\xa0\xa0\xff\xffhello\x05\x05\x05 \n " - - txt.print("trim:\n") - string.trim(tname1) - txt.chrout('[') - txt.print(tname1) - txt.print("]\n") - string.trim(tname2) - txt.chrout('[') - txt.print(tname2) - txt.print("]\n") - string.trim(tname3) - txt.chrout('[') - txt.print(tname3) - txt.print("]\n") - string.trim(tname4) - txt.chrout('[') - txt.print(tname4) - txt.print("]\n") + sub foo (str s2) { + str s = "irmen" + txt.print_uwhex(s, true) + txt.nl() + txt.print_uwhex(&s, true) + txt.nl() + txt.print_uwhex(&s[2], true) ; TODO doesn't print correctly in the AST! + txt.nl() + txt.nl() + txt.print_uwhex(s2, true) + txt.nl() + txt.print_uwhex(&s2, true) + txt.nl() + txt.print_uwhex(s2+2, true) + txt.nl() + txt.print_uwhex(&s2[2], true) ; TODO should be the same as the previous one! TODO doesn't print correctly in the AST! + txt.nl() } }