From 12712ef81206673dfb9fefb2f363e1429d0b42ae Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 27 Mar 2022 11:48:44 +0200 Subject: [PATCH] working on vm --- .../prog8/codegen/virtual/ExpressionGen.kt | 4 +-- compiler/res/prog8lib/virtual/syslib.p8 | 29 +++++++++++++++-- compiler/res/prog8lib/virtual/textio.p8 | 32 +++++++++---------- examples/test.p8 | 12 +++++++ 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/codeGenVirtual/src/prog8/codegen/virtual/ExpressionGen.kt b/codeGenVirtual/src/prog8/codegen/virtual/ExpressionGen.kt index 9017df8eb..96c1b3eb7 100644 --- a/codeGenVirtual/src/prog8/codegen/virtual/ExpressionGen.kt +++ b/codeGenVirtual/src/prog8/codegen/virtual/ExpressionGen.kt @@ -226,15 +226,13 @@ internal class ExpressionGen(val codeGen: CodeGen) { code += VmCodeInstruction(Instruction(Opcode.XOR, vmDt, reg1=resultRegister, reg2=leftResultReg, reg3=rightResultReg)) } "<<" -> { - // TODO check if shift amount works code += VmCodeInstruction(Instruction(Opcode.LSL, vmDt, reg1=resultRegister, reg2=leftResultReg, reg3=rightResultReg)) } ">>" -> { - // TODO check if shift amount works code += VmCodeInstruction(Instruction(Opcode.LSR, vmDt, reg1=resultRegister, reg2=leftResultReg, reg3=rightResultReg)) } "**" -> throw AssemblyError("** operator requires floating point ${binExpr.position}") - // TODO the other operators: "<<", ">>", "==", "!=", "<", ">", "<=", ">=" + // TODO the other operators: "==", "!=", "<", ">", "<=", ">=" else -> TODO("operator ${binExpr.operator}") } return code diff --git a/compiler/res/prog8lib/virtual/syslib.p8 b/compiler/res/prog8lib/virtual/syslib.p8 index a10c814cb..cba444dd2 100644 --- a/compiler/res/prog8lib/virtual/syslib.p8 +++ b/compiler/res/prog8lib/virtual/syslib.p8 @@ -8,10 +8,35 @@ sys { const ubyte target = 255 ; compilation target specifier. 64 = C64, 128 = C128, 16 = CommanderX16, 8 = atari800XL, 255 = virtual + ; SYSCALLS + ; 0 = reset ; resets system + ; 1 = exit ; stops program and returns statuscode from r0.w + ; 2 = print_c ; print single character + ; 3 = print_s ; print 0-terminated string from memory + ; 4 = print_u8 ; print unsigned int byte + ; 5 = print_u16 ; print unsigned int word + ; 6 = input ; reads a line of text entered by the user, r0.w = memory buffer, r1.b = maxlength (0-255, 0=unlimited). Zero-terminates the string. Returns length in r65535.w + ; 7 = sleep ; sleep amount of milliseconds + ; 8 = gfx_enable ; enable graphics window r0.b = 0 -> lores 320x240, r0.b = 1 -> hires 640x480 + ; 9 = gfx_clear ; clear graphics window with shade in r0.b + ; 10 = gfx_plot ; plot pixel in graphics window, r0.w/r1.w contain X and Y coordinates, r2.b contains brightness + + const ubyte SC_RESET = 0 + const ubyte SC_EXIT = 1 + const ubyte SC_PRINT_C = 2 + const ubyte SC_PRINT_S = 3 + const ubyte SC_PRINT_U8 = 4 + const ubyte SC_PRINT_u16 = 5 + const ubyte SC_INPUT = 6 + const ubyte SC_SLEEP = 7 + const ubyte SC_GFX_ENABLE = 8 + const ubyte SC_GFX_CLEAR = 9 + const ubyte SC_GFX_PLOT = 10 + sub reset_system() { ; Soft-reset the system back to initial power-on Basic prompt. - ; TODO + syscall(SC_RESET) } sub wait(uword jiffies) { @@ -38,7 +63,7 @@ sys { sub exit(ubyte returnvalue) { ; -- immediately exit the program with a return code in the A register - ; TODO + syscall1(SC_EXIT, returnvalue) } } diff --git a/compiler/res/prog8lib/virtual/textio.p8 b/compiler/res/prog8lib/virtual/textio.p8 index 642ccc9a5..86dcc8b0a 100644 --- a/compiler/res/prog8lib/virtual/textio.p8 +++ b/compiler/res/prog8lib/virtual/textio.p8 @@ -87,14 +87,14 @@ sub print_uwhex (uword value, ubyte prefix) { sub print_uw0 (uword value) { ; ---- print the uword value in decimal form, with left padding 0s (5 positions total) ; TODO use conv module? - ubyte tenthousands = value / 10000 as ubyte - value -= tenthousands * 10000 - ubyte thousands = value / 1000 as ubyte - value -= thousands * 1000 - ubyte hundreds = value / 100 as ubyte - value -= hundreds*100 - ubyte tens = value / 10 as ubyte - value -= tens*10 + ubyte tenthousands = (value / 10000) as ubyte + value -= 10000*tenthousands + ubyte thousands = (value / 1000) as ubyte + value -= 1000*thousands + ubyte hundreds = (value / 100) as ubyte + value -= 100 as uword * hundreds + ubyte tens = (value / 10) as ubyte + value -= 10*tens chrout(tenthousands+'0') chrout(thousands+'0') chrout(hundreds+'0') @@ -104,14 +104,14 @@ sub print_uw0 (uword value) { sub print_uw (uword value) { ; ---- print the uword in decimal form, without left padding 0s - ubyte tenthousands = value / 10000 as ubyte - value -= tenthousands * 10000 - ubyte thousands = value / 1000 as ubyte - value -= thousands * 1000 - ubyte hundreds = value / 100 as ubyte - value -= hundreds*100 - ubyte tens = value / 10 as ubyte - value -= tens*10 + ubyte tenthousands = (value / 10000) as ubyte + value -= 10000*tenthousands + ubyte thousands = (value / 1000) as ubyte + value -= 1000*thousands + ubyte hundreds = (value / 100) as ubyte + value -= 100 as uword * hundreds + ubyte tens = (value / 10) as ubyte + value -= 10*tens if tenthousands goto print_tenthousands if thousands diff --git a/examples/test.p8 b/examples/test.p8 index 3230a8ea8..c7a0b6a18 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,6 +6,18 @@ main { sub start() { txt.clear_screen() txt.print("Welcome to a prog8 pixel shader :-)\n") + ubyte bb = 10 + uword ww = 123 + bb <<= 4 + ww <<= 5 + txt.print("bb=") + txt.print_ub(bb) + txt.nl() + txt.print("ww=") + txt.print_uw(ww) + txt.nl() + sys.exit(99) + syscall1(8, 0) ; enable lo res creen ubyte shifter